I need to display an image when a user is online. Like the button of skype that it say if the user is online on skype's system.

How can I "check" if a user is online ? Is there a light module or I need to create a new one ? I don't need any type of block o stats about this user, only know if he is online or not.


  • Take a look at drupal.stackexchange.com/questions/14421/… – kalabro Jan 9 '13 at 10:17
  • What do you mean by 'online' exactly? If the user is on your site there's a pretty good chance they're online :) Do you mean "Check if user is logged in"? – Clive Jan 9 '13 at 10:24
  • I mean that I need to know if user with uid FOO do some operation in the last temporal window. "Operations" is like view page, update profile, comment a node and so on. I don't need what he is do, only know if he do something (in a temporal window like now() -5 mins). Thanks. – ZioBudda Jan 10 '13 at 10:31

For readability, and simplicity, I like to use the core API:

Check if current user is authenticated



if (user_is_logged_in()) {
  // User is authenticated
  print $content;

You can also use

if (!user_is_logged_in()) {
  // User is anonymous
  print $content;

though there's even a user_is_anonymous() function

if (user_is_anonymous()) {
  // User is anonymous
  print $content;

Check if particular user is logged in

If you want to check if a particular user is online

You can use Rules, or hook_user_login to respond to login event

EXAMPLE_user_login(&$edit, $account){
  /* do stuff */

or query the session table for that user session uid

// 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();
| improve this answer | |

If you want to show an image for each logged-in user, then you can use code similar to the one used for the block showing the list of currently logged-in users. For Drupal 7, the code would be similar to the following one.

function mymodule_logged_in_users($delay = 900, $max_users = 10) {
  $items = array();
  $interval = REQUEST_TIME - $delay;

  // Perform database queries to gather online user lists. We use s.timestamp
  // rather than u.access because it is much faster.
  $authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField();

  if ($authenticated_count) {
    $items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();

  return $items;

$items contains the list of users who are logged-in in the specified interval.

| improve this answer | |

This function is based on the code given by kiamlaluno. I needed a simple way to check if a user is online given the user id. Below is the code that worked for me.

function user_is_online($uid, $delay = 900) {

  $interval = REQUEST_TIME - $delay;
  $ret = db_query('SELECT 
    MAX(s.timestamp) AS max_timestamp
    FROM {users} u 
    INNER JOIN {sessions} s ON u.uid = s.uid 
    WHERE s.timestamp >= :interval 
    AND u.uid = :uid
    GROUP BY u.uid', array(':interval' => $interval, ':uid' => $uid))->fetchAll();

  if(count($ret)) return TRUE;

  return FALSE;

| improve this answer | |

You can add to your template file the next check:

global $user;
if ($user->uid)
    print "User Online";
| improve this answer | |

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.