Is there any way to display just the number of online users and unregistered users?

For example, a snippet that outputs something like this:

There are currently X registered users and X visitors online.

I'm not looking for it to display any avatars or names or anything, all I want is the number.

Comments

WorldFallz’s picture

There's lots of good snippets in the snippet pages: http://drupal.org/node/66638

taz3r’s picture

Thanks, but I've already seen/tried those.
Not what I was looking for.

WorldFallz’s picture

well, they do what you asked for in your op-- there's no way to make alternative suggestions without more info. "not what i was looking for" doesn't tell us anything.

taz3r’s picture

No they don't. The drupal 4 and 5 versions don't work for me. And the Drupal 6 version outputs a list of users that are online and the guest. I'm simply looking for a number for each.

Anonymous’s picture

If you enable the 'users' 'users stats' and 'tracker' module (I believe those are the three) you should see a block in your block menu name 'Who's Online'. I believe that is what you're looking for.

taz3r’s picture

Yeah, but I'm only looking to display the number of users online. I don't need a list of their names/avatars.

autoberater’s picture

Hello taz3r!

I have exact the same problem, i only want the number of online users und guests!
No names and pictures, only the two numbers!
Like in the headline of the standart Drupal Block "who is online".
I tested the following http://www.drupalcenter.de/node/24081#comment-85462 but it does not work for me in Drupal 6!

Has somebody an idea please?

greetz

autoberater’s picture

Has someone an idea please?
The following Code counts the total registered users on my side, but how can i count the currently guests and online users?

  $count[users] = db_result(db_query("SELECT COUNT(*) FROM {users}"));

  $output .= t("%count_users  Users", array('%count_users' => $count[users]));
  return $output;

Please help!
Thanks!

dalegrebey’s picture

Drupal 6.x Example:
16 of 2,073 registered Users and 31 Guests online.

I use this on a regular basis in my theme_preprocess_page(&$variables) function

Template.php:

<?php
function theme_preprocess_page(&$variables) { // replace "theme" with the name of your template

// set them amount of time that lapses for users to be "online"
$interval = time() - variable_get('user_block_seconds_online', 900); // 15 mins in seconds

// total number of registered users online
$users_online = db_result(db_query('SELECT COUNT(uid) FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', $interval));  

// total number of all users registered
$result = db_query("SELECT 1 FROM {users} WHERE status = 1 AND uid <> 0");
$users_total = number_format(db_affected_rows($result)); // total number of users w/ formatting

// total number of anonymous guests visiting the website
$users_guests = db_result(db_query('SELECT COUNT(hostname) FROM {sessions} WHERE timestamp >= %d AND uid = 0', $interval));

// assign all data to the users variable and pass to page.tpl.php
$variables['users'] = '<span class="highlight">' . $users_online . '</span> of <span class="highlight">' . $users_total . '</span> and   <span class="highlight">' . $users_guests . '</span> guests'; // x of y Users and z guests online

}
?>

page.tpl.php:

<?php echo $users; ?>

an example of this can be found: http://www.sceneorlando.com/

All I've done here is: 1.) Query the database for the correct results (users online, total number of users, guests online) 2.) wrap the results in a few span tags so that i can "highlight" the statistics, 3.) Add results to a variable ($variables['users']) passing this variable to the page.tpl.php template and of course 4.) print this out in my page.tpl.php file

...remember to clear cache (/admin/settings/performance)

autoberater’s picture

Yes, that´s it! Thanks a lot, it works!
But finally one last question, how can i get this query in an drupal block?

dalegrebey’s picture

A very simple way would be to create a block (/admin/build/block) enable your PHP filter (/admin/build/modules) and than just copy the code (not including the function { }) into said block. ...make sure that your input filter in your new block is set to php (as opposed to filtered html, etc).

dalegrebey’s picture

Copy from this comment:
// set them amount of time that lapses for users to be "online"

To this comment:
// x of y Users and z guests online

dalegrebey’s picture

Take that back... you have one more step.

This line of code:
<?php $variables['users'] = '<span class="highlight">' . $users_online . '</span> of <span class="highlight">' . $users_total . '</span> and <span class="highlight">' . $users_guests . '</span> guests'; // x of y Users and z guests online ?> needs to be fixed.

It should look like:
<?php echo '<span class="highlight">' . $users_online . '</span> of <span class="highlight">' . $users_total . '</span> and <span class="highlight">' . $users_guests . '</span> guests'; // x of y Users and z guests online ?>

All you are doing here is telling it print the variables (instead of passing it to page.tpl.php via $variables['users']

dalegrebey’s picture

...Just in case that was confusing, here is what needs to be copied into your block.

<?php
// set them amount of time that lapses for users to be "online"
$interval = time() - variable_get('user_block_seconds_online', 900); // 15 mins in seconds

// total number of registered users online
$users_online = db_result(db_query('SELECT COUNT(uid) FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', $interval)); 

// total number of all users registered
$result = db_query("SELECT 1 FROM {users} WHERE status = 1 AND uid <> 0");
$users_total = number_format(db_affected_rows($result)); // total number of users w/ formatting

// total number of anonymous guests visiting the website
$users_guests = db_result(db_query('SELECT COUNT(hostname) FROM {sessions} WHERE timestamp >= %d AND uid = 0', $interval));

// assign all data to the users variable and pass to page.tpl.php
echo '<span class="highlight">' . $users_online . '</span> of <span class="highlight">' . $users_total . '</span> and   <span class="highlight">' . $users_guests . '</span> guests'; // x of y Users and z guests online
?>
autoberater’s picture

Yes, thats it;)

Thank you!

JohnnyBeGood-dupe2’s picture

You don't need any interval of time.
You don't want to show the number of users who have visited the site in the last 15 minutes (unless you intend to impress the visitors with a bigger, but false value).
You want to show the number of users who are online "at the moment".

The SESSIONS table tell us exactly what you want to know.
Modify your code to the following and you get what you exactly want.

<?php

// total number of registered users online
$users_online = db_result(db_query('SELECT COUNT(*) FROM {sessions} WHERE uid not in (%d, %d)', 0, 1));

// total number of anonymous guests online
$users_guests = db_result(db_query('SELECT COUNT(*) FROM {sessions} WHERE uid = %d', 0));

print 'Registered: <span class="highlight">' . $users_online . '<br></br>Guests: <span class="highlight">' . $users_guests . '</span>'; 

?>

Regards

Katrina B’s picture

I'm not a programmer, coder, or developer, so I don't know how this problem could be resolved ... but one potential problem I see with this is that one user can generate more than one session. Which would probably explain why I have a great discrepancy between the results of this PHP block and a View I have set up to give me the usernames and IP addresses of online (registered) users.

The PHP block tells me that I have 62 registered users online. My View, however (which I have set up as Distinct, to list each user only once), lists just three registered users (myself included).

Any thoughts? Suggestions?

polishyourimage’s picture

is there a Drupal 7 version of this?

BigMike’s picture

I recently began using D7 and also missed the count of users that I had in D6's admin menu module. So I added it like so:

/**
 * Implements hook_admin_menu_output_build().
 *
 * Add count of users and guests to admin menu's People title.
 */
function my_module_admin_menu_output_build(array &$content) {
  // Return if menu is not present.
  if (!isset($content['menu']))
    return;
  // Return if user does not have access.
  if (!user_access('access administration menu'))
    return;
  $online_guests = db_query('SELECT COUNT(*) FROM {sessions} WHERE uid = :uid AND timestamp > :time', array(':uid' => 0, ':time' => time() - 900))->fetchField();
  $online_users = db_query('SELECT COUNT(*) FROM {sessions} WHERE uid != :uid AND timestamp > :time', array(':uid' => 0, ':time' => time() - 900))->fetchField();
  // Inject count to title "People"
  $content['menu']['admin/people']['#title'] = 'People (' . $online_guests . ', ' . $online_users . ')';
}

Note: I've designed this for use with the "Admin menu" module (https://www.drupal.org/project/admin_menu) v7.x-3.0-rc5 and Drupal v7.50.

This outputs the Admin Menu like so:
Content Structure Appearance People (71, 15) Modules Configuration Reports

...showing that I have 71 guests and 15 users on my site.

Notes:
1) I've been playing around with different cache settings to authenticated users and despite now being logged in at work, my account from my home network last night is still listed in my sessions table. So I added a timer exclusion limit of 15 minutes. Therefore based on your site's caching the counts may or may not be a true instantaneous count of your visitors.
2) Indifferent to other code mentioned here I'm including myself (user 1) because I'm a logged in user too!

Make sure you clear your administration menu cache for this change to take effect!

Regards,
BigMike

BeaPower’s picture

Thanks so much for this! How about if there is only one member online, how can I display the text "member," but if there is more than 1 member, such as 2, than display the text "members"?

ahmi’s picture

Is there a code for drupal 7.
It didn't worked with 7.41 core.

plato1123’s picture

I haven't tried it yet but this shows promise
https://www.drupal.org/project/user_stats

Provides commonly requested user statistics for themers, IP address tracking and Views integration. Statistics are:

days registered;
join date;
days since last login;
days since last post;
post count;
login count;
user online/offline;
IP address;

So it seems like you could make a view of users including anonymous users, put it in a block, make the block accessible to anonymous users.