Is there an Drupal API to check whether a user is currently logged in or not, based on the user ID, such as is_user_login($uid)?


No, there is no function like is_user_login($uid), but you can see how the core block "Who's online" is created (source).
Drupal has a "sessions" table, where all current sessions are stored.


Drupal doesn't have such function, but you can use code similar to the following one.

function mymodule_user_logged_in($uid, $delay = 900) {
  $interval = REQUEST_TIME - $delay;
  return (boolean) db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid = :uid", array(':timestamp' => $interval, ':uid' => $uid))->fetchField();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.