简体   繁体   中英

Joomla 2.5 - Display Active Logged-In Users

I have grouped a number of users and created a page that is accessible to these group only.

While I understand there are solutions that suggest $user->guest to ascertain user statuses, it only print out all users with roles other than Guest.

Thus I have researched and found that we can counter check with session table.

Below is my code:

<?php
$registeredUsers = JAccess::getUsersByGroup(11);

jimport('joomla.access.access');
jimport('joomla.user.user');

$db = JFactory::getDBO();
$query = 'SELECT COUNT(userid) FROM #__session AS s WHERE s.userid = ' (int)$userid;
$db->setQuery($query);
$loggedin = $db->loadResult();

foreach($registeredUsers as $user_id) {
$user = JFactory::getUser($user_id);

if($loggedin) {
     echo $user->name."; ";
  }
}
?>

Unfortunately, it returns all users too.

Anyone care enough to take a look at this?

@emmanuel Thank you for the headstart. I have managed to find the complete solution after playing with the code for hours.

$registeredUsers = JAccess::getUsersByGroup(11);
$registeredUsersex = implode(" ",$registeredUsers);

if(!empty($registeredUsers)){

$db = JFactory::getDBO();
$query = 'SELECT username FROM #__session AS s WHERE s.userid IN ( ' . implode(',', $registeredUsers) . ' )';
$db->setQuery($query);
$db->setQuery($query);
$loggedin_users = $db->loadRowList();

print_r(array_values($loggedin_users));
}
?>

getUsersByGroup method returns an array of user ids contained in a Group. Please refer here (for j2.5 version) .

So you have to check in your query if userid exists in this array using MySQL IN and php explode functions.

I haven't tested that, but result has to be something like this:

<?php
$registeredUsers = JAccess::getUsersByGroup(11);

jimport('joomla.access.access');
jimport('joomla.user.user');

if(!empty($registeredUsers)){

   $db = JFactory::getDBO();
   $query = 'SELECT userid FROM #__session AS s WHERE s.userid IN ( ' . explode(',', $registeredUsers) . ' )';
   $db->setQuery($query);
   $loggedin_users = $db->loadObjectList();

   foreach($loggedin_users as $user_id){
      $user = JFactory::getUser($user_id->userid);
      echo $user->name."; ";
   }

}
?>

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