简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM