magento检测登录How to check if customer is already logged in from another location?

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 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 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;
}
 

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 is 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.

 

来源:http://www.blog.magepsycho.com/how-to-check-if-customer-is-already-logged-in-from-another-location/

 

 

你可能感兴趣的:(location)