Cluster
Cluster

Reputation: 65

Joomla 3.x - How to display that certain user is on site to a guest user

What I am trying to do is to display to an guest/visitor to a Joomla site that a certain member is currently logged in or not. What I have so far is this:

    //First assigned user object to $user variable
$user = & JFactory::getUser();
if($user->guest){
    //Check user id is zero, if it is zero means user not logged in Joomla
    if ($user->id == 638) {
        echo "online.";
    } else {
        echo "offline.";
    }
}

However this does not work. I have this method which works only for the person that matches the user id:

//First assigned user object to $user variable
$user = & JFactory::getUser();

//Check user id is zero, if it is zero means user not logged in Joomla
if ($user->id == 638) {
    echo "online.";
} else {
    echo "offline.";
    }

}

But I can't make the work for the guest user. Any help is greatly appreciated.

Upvotes: 0

Views: 246

Answers (1)

Kajal Pasha
Kajal Pasha

Reputation: 104

Joomla user object return current user's(who is browsing) data. You need to check session table's data. This table store session info.

$db     =& JFactory::getDBO();
$db->getQuery(true);
$query  = 'SELECT COUNT(userid) FROM #__session WHERE userid = 638';
$db->setQuery($query);
$loggedin   = $db->loadResult();
if($loggedin){
    echo "online.";
} else {
    echo "offline";
}

Upvotes: 2

Related Questions