Markus
Markus

Reputation: 452

Is there a function to retrieve all current logged-in users?

Is there a function to retrieve all current logged-in users for Drupal 7?

I tried to build a query based on Get all the logged-in user.

$query->join('users', 'u', 's.uid = u.uid'); //JOIN sessions with users
$query->fields('u',array('name')) //SELECT the fields from node
  ->orderBy('timestamp', 'DESC') //ORDER BY timestamp
  ->range(0,10); //LIMIT to 10 records
  ->execute()
  ->fetchAll();
$result = $query->execute();

I'm still missing something on to retrieve all names from $result as an array, and on how to limit the query to return the users who were active in the last 5 minutes.

I need to put these into an array with their usernames in #title and their user IDs in #href.

Upvotes: 0

Views: 82

Answers (1)

Fky
Fky

Reputation: 2173

Retrieve users logged in last 5 minutes :

$users_results = db_query('SELECT u.uid , u.name FROM users u 
                   WHERE ( UNIX_TIMESTAMP(NOW()) - u.access ) < (5*60)')
          ->fetchAllKeyed();
dsm($users_results);

EDIT

Like asked :

foreach($users_results as $uid => $name){
  $users[$name] = array('#title' => $name , '#href' => $uid);
}

Upvotes: 2

Related Questions