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.";
}
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…
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.
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.
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?
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