Search code examples
joomlajoomla-component

Joomla: How to check if another user (not current user) is online


this is probably really simple but I've gone round in circles trying to get it to work. My forte is HTML/CSS rather than PHP.

I have a 3rd party component that I am customising to show when the user who posted the ad is online. So far I have not been able to get it to work.

I have been passing the user ID I get from the component, to the Joomla user object.

$user =& JFactory::getUser($var_from_component);

I have then tried to check whether the guest flag is set, as I understand that it will be zero if the user is logged in but null if the user is not.

if($user->guest==0) {
  echo "User is online";
 } else {
  echo "User is offline";
 }

I have also tried with three equals signs as I want check the value is actually set at zero rather than it being null, so basically I have been messing around with variations of this method for too long. If anyone knows a better way to check then please say. I also tried to check the session table for the userid, but I am not that savvy with mySQL queries.

Thanks in advance!


Solution

  • You can query session table to check if user is logged in or not-

    $db     =& JFactory::getDBO();
    $query      = 'SELECT COUNT(userid) FROM #__session AS s WHERE s.userid = '.(int)$userid;
    $db->setQuery($query);
    $loggedin   = $db->loadResult();
    if($loggedin){
        //echo 'User is logged in';
    }