Apr 25, 2013
kalpesh

Magento: Check if any particular customer is currently logged in

Let’s say you want to check if any particular customer is currently logged in to your site or not. Or let’s check how many customers with their customer IDs and other activities are online on your store.

This little script will help you in finding all the currently active OR any particular customer(s) active in your store.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require "app/Mage.php";
umask(0);
Mage::app();

$collection = Mage::getModel('log/visitor_online')->prepare()->getCollection();

//Get all the customers that are logged in......
foreach($collection->getData() as $cust) {
        echo 'Customer ID: '.$cust['customer_id'] . '<br/>';
        echo 'Last URL visited: '.$cust['last_url'] . '<br/>';
        echo 'First visit: '.$cust['first_visit_at'] . '<br/>';
        echo 'Last visit: '.$cust['last_visit_at'] . '<br/>';
        echo '======================<br/>';
}

//Get any particular customer, if he's currently logged in or not.....
$collection->addFieldToFilter('customer_id', 5)->addCustomerData(); //5 is customer ID of customer you want to check
if($collection->count()) {
    echo 'Customer is logged in';
} else {
    echo 'Customer is NOT logged in';
}

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications