Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - 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!

like image 386
Melanie Avatar asked Feb 09 '11 20:02

Melanie


People also ask

How do I know if a Magento 2 customer is logged in?

Then you need to use Magento\Customer\Model\Session::isLoggedIn() to check if the customer is logged in or not.

How can I get customer data using customer ID in Magento 2?

Use the following code to load Customer by ID using Object Manager in your Magento store. $customerId=1; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerData = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);


1 Answers

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.

like image 147
Zebooka Avatar answered Sep 29 '22 12:09

Zebooka