Magento : How to check if admin is logged in withi

2019-02-12 14:59发布

I'm creating a Magento Module. Within the controller, I want to check if an admin is logged in or not. So the controller only will be accessible if there is a logged in admin.

I'm trying to use this code on my controller.

Mage::getSingleton('core/session', array('name' => 'adminhtml')); 
$session = Mage::getSingleton('admin/session');

// Use the 'admin/session' object to check loggedIn status
if ( $session->isLoggedIn() ) {
   echo "logged in";
} else {
   echo "not logged in";
}

but I always get "not logged in", even if I'm already logged in to the magento admin.

Can anybody help me to resolve this issue?? any help will be much appreciated. Thanks

5条回答
小情绪 Triste *
2楼-- · 2019-02-12 15:17

As David Tay said, you should extend your controller from Mage_Adminhtml_Controller_Action.
Anyway, the shortest way to check if admin is logged in is to call this helper method:

Mage::helper('adminhtml')->getCurrentUserId();
查看更多
We Are One
3楼-- · 2019-02-12 15:20

That is really strange. I use almost exactly the same code and it works all the time:

//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()){
  //do stuff
}
else
{
  echo "go away bad boy";
}

Did you try var_dumping the $session variable? Maybe it will help you get on the right track...

查看更多
叼着烟拽天下
4楼-- · 2019-02-12 15:26

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楼-- · 2019-02-12 15:31

there is a new magento module, written by alan storm: https://github.com/astorm/Magento_CrossAreaSessions

查看更多
等我变得足够好
6楼-- · 2019-02-12 15:35

$user = Mage::getSingleton('admin/session');

if($user->getUser()->getUserId()) {
   // admin logged
}
else {
   // not admin logged
}
查看更多
登录 后发表回答