检查管理员和客户是否已登录
我有一台安装了Magento 1.4.0.1的Web服务器。 我有另一个网站与它共享凭据。 我已经设法检查客户是否登录(在更改Magento中的cookie位置之后),但是当我试图找出管理员是否登录时,情况变得复杂。我只能得到正确的答案对于我要求的第一个会话(客户或管理员,第二个从未登录)。
我怎样才能得到两个答案?
这里是我用来测试的代码:
require_once '../app/Mage.php';
umask(0) ;
Mage::app();
// Checking for customer session
Mage::getSingleton('core/session', array('name'=>'frontend') );
$session=Mage::getSingleton('customer/session', array('name'=>'frontend') );
if ($session->isLoggedIn()) {
echo "Customer is logged in";
} else {
echo "Customer is not logged in";
}
// Checking for admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml') );
$adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml'));
if($adminsession->isLoggedIn()) {
echo "Admin Logged in";
} else {
echo "Admin NOT logged in";
}
因此,像这样的代码,管理员从不登录。如果你先把管理员的部分放在首位,那么客户永远不会登录。似乎我在两个请求之间缺少一条线。
这可能与这个未回答的问题相同:Magento如何检查管理员是否在模块控制器中登录
这似乎是一个流行的问题,但我找不到合适的解决方案...
谢谢你的帮助!
你需要做的是切换会话数据。 你可以用下面的代码来做到这一点:
$switchSessionName = 'adminhtml';
$currentSessionId = Mage::getSingleton('core/session')->getSessionId();
$currentSessionName = Mage::getSingleton('core/session')->getSessionName();
if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) {
$switchSessionId = $_COOKIE[$switchSessionName];
$this->_switchSession($switchSessionName, $switchSessionId);
$whateverData = Mage::getModel('mymodule/session')->getWhateverData();
$this->_switchSession($currentSessionName, $currentSessionId);
}
protected function _switchSession($namespace, $id = null) {
session_write_close();
$GLOBALS['_SESSION'] = null;
$session = Mage::getSingleton('core/session');
if ($id) {
$session->setSessionId($id);
}
$session->start($namespace);
}
我从另一个角度(试图从管理层登录客户)发现“bug-feature”,但仍然找到原因。
问题在于session_name()函数。 如果你去了Mage_Core_Model_Session_Abstract_Varien,你会看到会话对象使用了标准的PHP会话函数,PHP不能同时处理两个会话。
您的adminside会话ID存储在cookie adminhtml中,而对于客户端,您的会话ID位于前端Cookie中。 然后在adminside中,您有由adminhtml cookie初始化的会话ID。 在管理方面,您的客户/会话对象存储在PHP会话中的$ _SESSION ['customer'](未检查确切键)之类的内容中,以存储在adminhtml cookie中。 这意味着客户/会话对象在magento的管理员和客户端部分内引用不同的会话。
这是我用的..
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
$session = Mage::getSingleton('admin/session');;
if (!$session->getUser())
{
die("You aren't an admin!");
}
链接地址: http://www.djcxy.com/p/50697.html
上一篇: Checking if an Admin and a Customer are logged in
下一篇: Is there a tool for Django project structure/information flow visualization?