Magento-检查管理员和客户是否已登录 [英] Magento - Checking if an Admin and a Customer are logged in

查看:85
本文介绍了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";
}

因此,使用这样的代码,管理员永远不会登录.如果您首先放置有关管理员的部分,那么客户永远都不会登录.似乎我在两个请求之间缺少一行. /p>

这可能是与以下未解决问题相同的问题:解决方案

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

$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);
}

I have a web server with Magento 1.4.0.1 installed. I have another web site that shares credential with it. I've managed to check if the customer is logged in or not (after having changed the cookies location in Magento), but things got complicated when I also tried to figure out if an admin was logged in. I can only get the proper answer for the first session I asked for (either the customer OR the admin, the second one is NEVER logged in).

How can I have both answers?

Here is the code I'm using to test that out:


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";
}

So with the code like this, the admin is never logged in. If you put the part about the admin first, then the customer is never logged in. It seems like I'm missing a line between the two requests.

This may be the same problem than this unanswered question: Magento how to check if admin is logged in within a module controller

This seems like a popular problem, but I could not find the proper solution...

Thanks for your help!

解决方案

What you need to do is switch the session data. You can do this with the following code:

$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);
}

这篇关于Magento-检查管理员和客户是否已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆