Custom "Who's Online" block

This is a customization of the "Who's Online" block (it should help you to change whatever else you would like to change as well). This one just adds the total number of registered users in addition to telling who is online.

Drupal 5

<?php
$number
= db_result(db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1'));

        if (
user_access('access content')) {
         
// Count users with activity in the past defined period.
         
$time_period = variable_get('user_block_seconds_online', 900);

         
// Perform database queries to gather online user lists.
         
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
         
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
         
$total_users = db_num_rows($users);

         
// Format the output with proper grammar.
echo "Out of $number registered users ";
          if (
$total_users == 1 && $guests->count == 1) {
           
$output = t('%members and %visitors online.', array('%members' => format_plural($total_users, 'there is currently 1 user', 'there are currently @count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
          }
          else {
           
$output = t('there are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
          }

         
// Display a list of currently online users.
         
$max_users = variable_get('user_block_max_list_count', 10);
          if (
$total_users && $max_users) {
           
$items = array();

            while (
$max_users-- && $account = db_fetch_object($users)) {
             
$items[] = $account;
            }

           
$output .= theme('user_list', $items, t('Online users'));
          }

        }
        return
$output;
?>

Drupal 4.7

<?php
$number
= db_result(db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1'));

        if (
user_access('access content')) {
         
// Count users with activity in the past defined period.
         
$time_period = variable_get('user_block_seconds_online', 900);

         
// Perform database queries to gather online user lists.
         
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
         
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
         
$total_users = db_num_rows($users);

         
// Format the output with proper grammar.
echo "Out of $number registered users ";
          if (
$total_users == 1 && $guests->count == 1) {
           
$output = t('%members and %visitors online.', array('%members' => format_plural($total_users, 'there is currently 1 user', 'there are currently %count users'), '%visitors' => format_plural($guests->count, '1 guest', '%count guests')));
          }
          else {
           
$output = t('there are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '%count users'), '%visitors' => format_plural($guests->count, '1 guest', '%count guests')));
          }

         
// Display a list of currently online users.
         
$max_users = variable_get('user_block_max_list_count', 10);
          if (
$total_users && $max_users) {
           
$items = array();

            while (
$max_users-- && $account = db_fetch_object($users)) {
             
$items[] = $account;
            }

           
$output .= theme('user_list', $items, t('Online users'));
          }

        }
        return
$output;


?>

Most users ever online

gaijinu - January 23, 2008 - 18:23

Right before

        }
        return $output;

add:

        $total_users_online = $total_users + $guests->count;
        if (variable_get('most_users_online_ever', '') < $total_users_online)  {
             variable_set('most_users_online_ever', $total_users_online);
             variable_set('most_users_online_ever_time', time());
        }
        $output .=  "Most users ever online was " . variable_get('most_users_online_ever', '');
        $output .=  " on " . format_date(variable_get('most_users_online_ever_time', ''), 'medium', '');

It will print something like:
Most users ever online was 66 on 01/24/2008 - 02:00

Tested in Drupal 5.6.

You can change "<" to "<=" if you'd like to show the most recent date.

Cheers!

--
Muay Thai Singapore | The Contender Asia Fans
Sailing Singapore

Variation - showing IP and domain of guest

joel box - March 23, 2008 - 13:19

Hi I made a bit of a variation, that is sometimes useful for the administrators.
(I haven't quite figured out yet how to call on the theming functions, so it includes some html formatting you might need to change.

<?php
$number
= db_result(db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1'));

        if (
user_access('access content')) {
         
// Count users with activity in the past defined period.
         
$time_period = variable_get('user_block_seconds_online', 900);

         
// Perform database queries to gather online user lists.
         
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
         
$guests_hostname = db_query('SELECT hostname FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period);
         
$total_guests = db_num_rows($guests_hostname);

         
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
         
$total_users = db_num_rows($users);

         
// Display a list of currently online users.
         
$max_users = variable_get('user_block_max_list_count', 10);
          if (
$total_users && $max_users) {
           
$items = array();
            while (
$max_users-- && $account = db_fetch_object($users)) {
             
$items[] = $account;
            }
           
$output.="<h7>Users</h7>";
           
$output .= theme('user_list', $items, NULL);
          }

         
// Display a list of currently online guests.
         
if ($total_guests) {
           
$output.="<div class=\"item-list\"><h7>Guests</h7><ul><fine>";
           
$guestitems = array();

            while (
$guests-- && $account = db_fetch_object($guests_hostname)) {
             
$guestitems[] = $account->hostname;
             
$output.="<li><a title=\"Go to address\" href=\"http://$account->hostname\">$account->hostname</a>
              <a title=\"Go to address\" href=\"http://"
.gethostbyaddr($account->hostname)."\">".gethostbyaddr($account->hostname)."</a>       "
            }
          
$output.="</fine></ul></div>";
          }
        }
        return
$output;
?>

which gives as result:

Who is online
Users
* administrator
Guests
* 192.168.0.50 Saturnus

of course the 192.. Ip will change to any guests IP looking at your site.

Code modification for Drupal 6

VladSavitsky - April 23, 2008 - 11:16

I have fixed joel box's code to work in Drupal 6:

<?php
$number
= db_result(db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1'));

        if (
user_access('access content')) {
         
// Count users with activity in the past defined period.
         
$time_period = variable_get('user_block_seconds_online', 900);

         
// Perform database queries to gather online user lists.
         
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
         
$guests_hostname = db_query('SELECT hostname FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period);
         
$total_guests = mysql_num_rows($guests_hostname);

         
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
         
$total_users = mysql_num_rows($users);

         
// Display a list of currently online users.
         
$max_users = variable_get('user_block_max_list_count', 10);
          if (
$total_users && $max_users) {
           
$items = array();
            while (
$max_users-- && $account = db_fetch_object($users)) {
             
$items[] = $account;
            }
           
$output.="<h7>Users</h7>";
           
$output .= theme('user_list', $items, NULL);
          }

         
// Display a list of currently online guests.
         
if ($total_guests) {
           
$output.="<div class=\"item-list\"><h7>Guests</h7><ul><fine>";
           
$guestitems = array();

            while (
$guests-- && $account = db_fetch_object($guests_hostname)) {
             
$guestitems[] = $account->hostname;
             
$output.="<li><a title=\"Go to address\" href=\"http://$account->hostname\">$account->hostname</a>
              <a title=\"Go to address\" href=\"http://"
.gethostbyaddr($account->hostname)."\">".gethostbyaddr($account->hostname)."</a>       "
            }
          
$output.="</fine></ul></div>";
          }
        }
        return
$output;
?>

Function db_num_rows() exists only in Drupal 5.x and below.
So this function was replaced with mysql_num_rows() according to http://api.drupal.org/api/function/db_num_rows/5.

-----------------
drupalcookbook.ru

hmm.. really?

Chippe - June 9, 2008 - 00:10

Doesn't work for me.
I thought db_num_rows -> db_result but Im not sure.

It does not work in my D6.2

koyauni - June 12, 2008 - 16:04

I did try the code but it did not work, nothing shows! :(

When enabling in 6.2

bigmack83 - June 20, 2008 - 04:54

When enabling in 6.2 i get the following error and also does not show in the block

    * warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\d6test\includes\common.inc(1537) : eval()'d code on line 11.
    * warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\d6test\includes\common.inc(1537) : eval()'d code on line 14.

I am running WAMP with the latest version of everything. I am also using the theme Miranelli if that matters...

 
 

Drupal is a registered trademark of Dries Buytaert.