Skip to content

Magento: Check if an admin is logged in or not

If you are writing a custom module, and you want it to be accessed only by admins, then you possibly need the following code in your controller.

//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  echo "Admin Logged in";
}
else
{
  echo "You need to be logged in as an admin.";
}
Dipin Krishna

Written by Dipin Krishna

Senior full-stack engineer — 15 years across Django, Laravel, SwiftUI and the infrastructure underneath. Available for contract work.

Work with me →

6 comments

  1. # Ensure we are in the admin session namespace for checking the admin user..
    Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();
     
    $admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
     
    # ..get back to the original.
    Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();
  2. Is there any way to get a block on the frontend that could check if the user is logged into the admin? My only idea is to wrap it in an iframe with a custom controller that extends Mage_Adminhtml_Controller_Action — but I’d rather not use an iframe if I don’t have to…

  3. Make sure that your module’s adminhtml controller is extending Mage_Adminhtml_Controller_Action. You can’t check if an admin is logged in from a front end controller.

  4. Make sure that your module’s adminhtml controller is extending Mage_Adminhtml_Controller_Action. You can’t check if an admin is logged in from a front end controller.

  5. Hello, thanks for this code.
    I tried to implement it in frontend, but it return always the message “You need to be logged in as an admin.” Any help?

  6. Hi, thanks for this snipped.
    I tried to put the code in my header.phtml but it return always “You need to be logged in as an admin.” even if I’m logged in the admin panel.
    What’s wrong? I’m running on Magento 1.5.

    Thank you

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.