In order to check whether the current user is logged in or not, we can easily find out using the following code:
if(Mage::getSingleton('customer/session')->isLoggedIn()){
//customer is logged in
}
Note: This code only checks the current session of a customer using the current browser.
Case:
Suppose if a user with email: [email protected] is already logged in via one browser, the same user tries to login via another browser or another user who knows the username/password tries to log in from another location then how will you check if the 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;
}
Notes: If you browse the table: log_visitor_online you can see it stores the details of currently logged in customers and guests.
Above code just tries to check if current customer details are on the table or not. If customer data is present it means he/she is already logged in and online else this will be his/her fresh login.