Drupal: List of IP & Domains of Users Who Are Currently Online
Problem
Get information about users who are currently on the site
Solutions
- Enable the standard block "Who’s online"
- Create your own block with the necessary information
Create block
This code shows IP addresses and domains of anonymous users (guests).
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));
$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;
?>
Drupal 6:
To adopt this snippet for D6, you should replace db_num_rows() function to mysql_num_rows() in accordance with mysql_num_rows() API function.
Attention:
Do not forget to adjust access rights to the block with this info. Users want be anonymous and showing their info can upset them…