Magento - 检查管理员和客户是否已登录

Mel*_*nie 7 php session magento

我有一个安装了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";
}
Run Code Online (Sandbox Code Playgroud)

因此,使用这样的代码,管理员永远不会登录.如果您首先将部分放在管理员身上,那么客户永远不会登录.看起来我在两个请求之间缺少一条线.

这可能与这个未解决的问题相同:Magento如何检查管理员是否在模块控制器中登录

这似乎是一个受欢迎的问题,但我找不到合适的解决方案......

谢谢你的帮助!

小智 2

您需要做的是切换会话数据。您可以使用以下代码来执行此操作:

$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);
}
Run Code Online (Sandbox Code Playgroud)