Display a customizable "this user is [online/offline]" status message in the User Profile page

Last modified: August 26, 2009 - 22:15

Note from the moderator: Thank you for sharing the snippet with the Drupal community. In its current state the snippet might present security risks. See Writing secure code for a background on the most common problems.

Specifically, the second snippet

  • allows SQL injection because it uses user supplied data directly inside an SQL query without further checks. Proper use of Drupals database abstraction layer can prevent these attacks.

Example of db_query usage:

<?php
  db_query
("SELECT * FROM {users} WHERE uid = $uid"); // INSECURE
 
db_query("SELECT * FROM {users} WHERE uid = %d", $uid);
?>

--

PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet adds a This user is [online/Offline] status message in the user profile page.

Dependencies: profile.module must be installed and enabled.

Usage

  • For use in your user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the code into your user_profile.tpl.php file
  • Select appropriate snippet for the versionof Drupal you are using
  • Change the div class name or the message text to suit.

Snippet for Drupal 4.5 and 4.6

<?php
print "<div class=\"fields\">";
print
"Online/Offline Status";
$time_period = variable_get('user_block_seconds_online', 2700);
$uid = arg(1); // get the current userid that is being viewed.
$users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid = %u GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period, $uid);
         
$total_users = db_num_rows($users);

          if (
$total_users == 1) {
           
$output = t('This user is currently online');
          }
          else {
           
$output = t('This user is currently offline');        
          }

              print
$output;
                       print
"</div>";
?>

Snippet for Drupal 4.7

Thanks to rocketman528 for this snippet.

<?php
print "<div class=\"fields\">";
print
"Online/Offline Status";
$time_period = variable_get('user_block_seconds_online', 2700);
$uid = arg(1); // get the current userid that is being viewed.
$users = db_query("SELECT uid, name, access FROM {users} WHERE access >= %d AND uid = $uid", time() - $time_period);
         
$total_users = db_num_rows($users);

          if (
$total_users == 1) {
           
$output = t('This user is currently online');
          }
          else {
           
$output = t('This user is currently offline');     
          }

              print
$output;
                      print
"</div>";
?>

Small Update for Drupal 5.1 and usernode.module w/ Questions

WhatTheFawk - February 15, 2007 - 22:30

I'm using this in Drupal 5.1 with the usernode.module. I had to change a few things in this snippet because the way it's getting the uid by using arg(1) conflicted with the new usernode.module. The usernode.module, (if used with the template.php code that comes with it), overwrites user/[uid] from all links and takes you to usernode/[usernodeid], BUT usernodeid which is what arg(1) will grab is not the uid so $uid = arg(1); wasn't working.

Change:

$uid = arg(1); // get the current userid that is being viewed.
$users = db_query("SELECT uid, name, access FROM {users} WHERE access >= %d AND uid = $uid", time() - $time_period);

To:

$users = db_query("SELECT DISTINCT(uid), access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $user->uid);

It all works smoothly so far so I hope this is correct, I read above if you're using $user->uid or similar you shouldn't directly have uid = $user->uid in the SQL query, but instead should use db_query("SELECT foo FROM {bar} WHERE uid = %s",$user->uid); syntax, can anyone tell me if I'm doing this right?

Full Code [Drupal 5.1][ Makes User Online/Offline snippet compatible with usernode.module ]:

<?php
   
print "<div class=\"fields\">";
   
       
$time_period = variable_get('user_block_seconds_online', 600);
   
$users = db_query("SELECT DISTINCT(uid), access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $user->uid);
   
$total_users = db_num_rows($users);
        if (
$total_users == 1)
        {
           
$output = t($user->name.' is currently online');
        }
        else
        {
           
$output = t($user->name.' is currently offline');     
        }
       
    print
$output;
    print
"</div>";
?>

I also in $time_period I shortened 2700 which I think is 45 mins to 600 (10 mins), 45 mins seemed a bit long to me. I thought it was broken when I was checking the status of another user, but really I just hadn't waited 45 mins after logging off to check.

Missing global $user; in your code.

stevenjo - July 4, 2007 - 06:24

Add

<?php
global $user;
?>

right below <? tag to make it work.

Steven Jodistiro
http://www.groklight.com

It worked for me without the

Drupal_ - September 2, 2007 - 16:15

It worked for me without the global $user .. I am using 5.1

-----------------------------
PopularSikh - for the sikh inside you
http://www.popularsikh.com

Does this really indicate

ebeyrent - October 2, 2007 - 19:21

Does this really indicate that the user is currently logged in? Perhaps I don't understand the code, but it looks like you are making the assumption that after 45 (or 10) minutes, the user is not logged in anymore. Does the user record contain a flag that can be queried regarding whether a user is logged in or not?

Logged in

Michelle - October 27, 2007 - 14:48

This snippit has nothing to do with whether they are looged in. It is making a best guess as to whether they are online based on the time since last activity. Since people can stay logged in when they aren't using the site, being logged is irrelevent.

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

I agree, even if you log out

Chippe - June 9, 2008 - 10:45

I agree, even if you log out it will say you're online. While you're online, it seems like surfing the site is not enough to keep you active. You will have to make a post or something.

I made a small change so it'll work in D6.

BTW, Im still looking for a better way to show the online/offline status.
I think it should be based on if you are logged in or out but also on activness since you might leave the site without logging out and visiting a node = active.

Here is the code for Drupal 6 in case somebody wants it:

<?php
print "<div class=\"fields\">";
print
"Online/Offline Status";
$time_period = variable_get('user_block_seconds_online', 2700);
$uid = arg(1); // get the current userid that is being viewed.
$users = db_query("SELECT uid, name, access FROM {users} WHERE access >= %d AND uid = $uid", time() - $time_period);
         
$total_users = db_result($users);

          if (
$total_users == 1) {
           
$output = t('This user is currently online');
          }
          else {
           
$output = t('This user is currently offline');    
          }

              print
$output;
                      print
"</div>";
?>

What's the database query for?

Altaisoft - November 12, 2008 - 12:18

I'm using Drupal 6.6. In the user-profile.tpl.php file the $account variable is allowed. It contains all the data required.

<?php
  $time_period
= variable_get('user_block_seconds_online', 2700);
 
$uid = $account->uid; // get the current userid that is being viewed.
 
if ($account->access > time() - $time_period) {
    echo
'Online';
  }
  else {
    echo
'Offline';
  }
?>

This works for me pretty well.

 
 

Drupal is a registered trademark of Dries Buytaert.