Magento

How to Check customer or is logged in or not?

Source Code for check Customer:

if(!Mage::getSingleton('customer/session')->isLoggedIn())
{
 $this->_redirect('customer/account/login/');
}

Case:

Suppose if a user with email: customer@domain.com is already logged in via one browser, the same user tries to login via another browser or another user who knows the usename / password tries to login from another location then how will you check if customer with that email is already logged in?

Solution:

Well it’s simple enough.
You can create Event/Observer that hook into the event: customer_login and check as follows in the observer method:

$customer = $observer->getEvent()->getCustomer();
$log = Mage::getModel('log/visitor_online')->getCollection()->addFieldToFilter('customer_id', $customer->getId())->getFirstItem();
if($log->getId()){ //current user is already logged in / online from another location
    Mage::getSingleton('customer/session')->logout();
    $session->addError(Mage::helper('customer')->__('Customer with email (%s) is already logged in.', $customer->getEmail()));
    Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
    return;
}

Source Code for check Admin:


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

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

Leave your comments

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s