在外部脚本中从Magento登录用户ID-多个会话问题? [英] Getting logged in user ID from Magento in external script - multiple session issue?
问题描述
我可以成功登录用户在像这样的,
I can successfully log a user in like this,
// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();
// Log in user
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login($username, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
// redirect with session ID
header('Location: https://www.domain.com/?SID='.$session->getEncryptedSessionId());
exit;
和然后我能得到我需要使用任何用户数据,
and then I can get any user data I need using,
// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();
if(Mage::getSingleton('customer/session')->isLoggedIn())
echo Mage::getSingleton('customer/session')->getId();
else
echo 'Not logged in';
问题是,当用户通过网站上的表单登录而不是使用第一个块中的代码登录时,以上代码块返回未登录".
The problem is that the above block of code returns 'Not logged in' when a user has logged in via the form on the site as opposed to being logged in with the code in the first block.
看起来,两个日志的方法创建可以并行运行单独的会话.如果使用用户登录X网站的内置的形式,然后用户Y登录使用我的代码. - 两个用户登录该商店表示,欢迎用户X,但我的代码返回用户Y的ID
It appears that the two log in methods create separate sessions that can be run in parallel. If user X logs in using the site's built in form and then user Y logs in using my code - both users are logged in. The store says, welcome user X, but my code returns user Y's ID.
任何想法,这是怎么回事?我需要的用户能够登录两种方式,但需要能够得到他们的ID.
Any ideas as to what's going on? I need users to be able to log in either way, but need to be able to get their ID.
推荐答案
我还没有100%清除发生了什么事,但我已经解决了这个问题 - 有我做了
I'm still not 100% clear what was happening but I have solved the problem - there was two changes I made.
在 Magento管理>系统>配置>常规>网络>会话Cookie管理我将路径的Cookie 为/
在包含Magento应用程序的文件中,添加了一行代码
In my files where I included the Magento app I added a line of code
// Include Magento application
require_once ('../store/app/Mage.php');
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend')); // <- NEW LINE
// ... and so on ...
现在,不管用户如何被登录,我的外部代码和商店都同意是谁登录.
Now, regardless of how the user gets logged in, my external code and the store both agree on who is logged in.
我可以看到如何改变Cookie路径帮助,但如果任何人有,为什么额外的代码行的工作,任何的解释,将不胜感激.
I can see how changing the cookie path helped but if anyone has any explanation for why the extra line of code worked that would be appreciated.
干杯!
这篇关于在外部脚本中从Magento登录用户ID-多个会话问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!