Create a “Who is online” block in Drupal 7

0

Posted by fred | Posted in CMS, Code, Drupal | Posted on 18-03-2012

 

 

 

 

 

Based on this article, you can create a block in Drupal to show who is online. Create a block, use PHP Code as text format and paste the following code.

 

Note there was a bug in the line #7. In order to make it work, I had to changes the double quotes to single quotes.

 

<?php
/* Code based on http://www.phpcodester.com/2011/04/drupal-7-block-for-whos-online-with-guests/ */
if (user_access(‘access content’)) {
$spiders=array(‘google’, ‘fatlens’, ‘yahoo’, ‘altavista’, ‘yandex’, ‘baidu’, ‘bing’, ‘thefind’, ‘bot’, ‘spider’, ‘crawl’);
$interval = REQUEST_TIME – variable_get(‘user_block_seconds_online’, 900);
$authenticated_count = db_query(“SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0″, array(‘:timestamp’ => $interval))->fetchField();
$output=”<div class = ‘visitor-ips’>”;
$max_users = variable_get(‘user_block_max_list_count’, 10);
/*if ($authenticated_count && $max_users) {
$output.=”<h3>Registered Users</h3>”;
$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();
$output .= theme(‘user_list’, array(‘users’ => $items));
}*/
$guest_count = db_query(“SELECT COUNT(DISTINCT s.hostname) FROM {sessions} s WHERE s.timestamp >= :timestamp”, array(‘:timestamp’ => $interval))->fetchField();
$output.=”$guest_count visitor(s)”;
$sData=array(); $gData=array();
if ($guest_count && $max_users) {
$items = db_query_range(‘SELECT DISTINCT s.hostname, MAX(s.timestamp) AS max_timestamp FROM {sessions} s WHERE s.timestamp >= :interval ORDER BY max_timestamp DESC’, 0, 10000000, array(‘:interval’ => $interval))->fetchAll();
$i=0;
for ($i=0;$i<count($items);$i++){
$ips[$i]=$items[$i]->hostname;
}
$ips=array_unique($ips);
foreach ($ips as $ip){
$host=preg_replace(‘/[^a-zA-Z\.]/’, ”, gethostbyaddr($ip));
if ($host==”…”) $host=”Unknown Hostname”;
$spider=0;
foreach ($spiders as $spider){
if (strpos($host, $spider)!==false){
$spider=1;
break;
}
}
if ($spider==1){
$sData[]=array(‘ip’=>$ip, ‘host’=>$host);
}else{
$gData[]=array(‘ip’=>$ip, ‘host’=>$host);
}
}
}
if (isset($gData) && count($gData)>0){
// $output.=”<h3>Guests</h3>”;
$output.=”<ul>”;
$i=0;
foreach ($gData as $d){
$output.=”<li>”.$d['host'].’, ‘.$d['ip'];
$loc=unserialize(file_get_contents(‘http://www.geoplugin.net/php.gp?ip=’.$d['ip']));
if ($loc['geoplugin_city'].$loc['geoplugin_region']!=”){
$output.=”, “.$loc['geoplugin_city'].”, “.$loc['geoplugin_region'];
}
if ($loc['geoplugin_countryName']!=”){
$output.=”, “.$loc['geoplugin_countryName'];
}
$output.=”</li>”;
if ($i>$max_users){break;}
$i++;
}
$output.=”</ul>”;
}
if (isset($sData) && count($sData)>0){
$output.=”Spiders:”;
$i=0;
$output.=”<ul>”;
foreach ($sData as $d){
$output.=”<li>”.$d['host'].”, “.$d['ip'].”</li>”;
if ($i>$max_users){break;}
$i++;
}
$output.=”</ul>”;
}
$output.=” </div>”;
return $output;
}
?>

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Create a "Who is online" block in Drupal 7, 5.0 out of 5 based on 1 rating

Write a comment

Security Code: