Jun 19, 2011
kalpesh

Magento: Checking customer/admin is logged in or not

The most important part in magento development is to check whether customer is logged in or not in the system. So that you can show specific features and extra privileges to your customers. There are many things that are restricted to the guests, while members can see the special privilege things.

Also if the customer is not logged in, we can show a link like “Log in”; while for logged in customers, we can show link like “Log out”, “My account”, etc. This is already present in Magento, but we may want to add more features to our customers. So here is the code where you can check customer is logged in or not.

This will check whether the customer is logged in the magento system or not.

1
2
3
4
5
6
7
$session=Mage::getSingleton('customer/session', array('name'=>'frontend') );

if ($session->isLoggedIn()) {
    echo "Welcome, ".$session->getCustomer()->getName();
} else {
    echo "Not logged in";
}

This will check whether admin is logged in or not in a magento site.

1
2
3
4
5
6
7
$adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml'));

if($adminsession->isLoggedIn()) {
    echo "Welcome Admin";
} else {
    echo "Not logged in";
}

5 Comments

  • Very cool stuff from Magento developers’ team!! I think Magento team trying their best to give the best to the users. I think this code is useful and it’s really crucial to know visitors are logged in or not. Thanks :)

  • I suggest adding a “google+” button for the blog!
    Hellen

  • very good for know check cusromer login or not…
    but admin if else condition not work properly alway else condition execute..

    • Yes….facing same problem …

      I have put this code in frontside controller file… but not working….

      any other way to do it?

      I want admin session data at front side . magento 1.5.1.0

  • Is it possible ??
    Magento admin can login from another site or using api?

Leave a comment

 

Welcome to my Blog

This blog is merely to record the things that I find interesting. It can be technical, humor, news, creative or anything that deserves to be posted here. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications