There is a lot of questions how to check the current user is logged in, but I need to check another users (by uid for example). Is there any easy way to do it?
2 Answers
There aren't any functions in Drupal that I am aware of. You'll need to query the database.
$time_active = db_query("SELECT timestamp FROM {sessions} WHERE uid = :uid ORDER BY timestamp DESC LIMIT 1", array(":uid" => $uid))->fetchField();
That will fetch the last time that the user was active if they have a current session open.
-
A similar method is used by the core block "Who's online" (in example.com/modules/user/user.module)– UrsulaNov 27, 2013 at 22:33
Hope this will help you,
You can use sessions table. You can fetch a list of all logged in users by querying the sessions table
// Check if user $uid is online in the last 10 minutes
$online = db_query("SELECT uid FROM {sessions}
WHERE uid = :uid
AND timestamp >= :time", array(':uid' => $uid, ':time' => (REQUEST_TIME - 600))->fetchField();
Reference : Check if user is online