Checking if an Admin and a Customer are logged in

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

I've found that "bug-feature" from another angle of view (trying to login customer from adminside), but still found the cause.

The problem is with session_name() function. If you go to Mage_Core_Model_Session_Abstract_Varien you'll see there that the session object is using standart PHP session functions and PHP can't handle two sessions at the same time.

You session id for adminside is stored in cookie adminhtml, while for clientside your session id is in frontend cookie. Then in adminside you have session ID initialized by adminhtml cookie. When in adminside, your customer/session object is stored inside something like $_SESSION['customer'] (haven't checked exact key) inside PHP session for ID stored in adminhtml cookie. This means that customer/session object is refering to different sessions when inside admin and client parts of 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/50698.html

上一篇: Google Data API:如何对桌面应用程序进行身份验证

下一篇: 检查管理员和客户是否已登录