Tutorialsplane

Magento check if customer is logged in


Magento check if customer is logged in : While working with magento we sometimes need to decide whether user is logged from front end or not. Mage::getSingleton(‘core/session’, array(‘name’ => ‘frontend’)); is used to get the front end logged in user session in Magento 1.x but In magento 2.x object manager $objectManager->get(‘Magento\Customer\Model\Session’) is used to get the customer session. Here in this tutorial we are going to explain how you can check the customer login on magento 1 and magento 2 front end.


Magento check if customer is logged in | Magento 2

It is very simple to check the looged in customer in magento 2, using the below method you can check whether the customer is logged in or not anywhere in the application. We have used object manager to get the customer session.

Magento 2 check if frontend user is logged in:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer logged in
}else{
// customer not logged in.
}

Magento 1.x


In magento 1 you can check the customer is logged in simply as below –

Magento check if frontend user is logged in:


Mage::getSingleton('core/session', array('name' => 'frontend'));

$customerSession = Mage::getSingleton("customer/session");

if($customerSession->isLoggedIn()) {
  echo "Is Logged In";
} else {
   echo "User Not Logged";
}

Using the above method you can check that the user is logged in from front end or not.