Skip to main content

Users online now...

Submitted by Anonymous on

<?php
/* Code based on http://www.phpcodester.com/2011/04/drupal-7-block-for-whos-online-with-… */
if (user_access('access content')) {
$spiders = array('google', 'fatlens', 'yahoo', 'altavista', 'yandex', 'baidu', 'bing', 'thefind', 'bot', 'spider', 'crawl');
$now = $_SERVER['REQUEST_TIME'];
$interval = $now - 3600 * 6 /*variable_get('user_block_seconds_online', 3600 * 6)*/;
$output = "<div class='visitor-ips'>";
$max_users = 12; //variable_get('user_block_max_list_count', 10);
/*$authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField();
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 {accesslog} s WHERE s.timestamp >= :timestamp", array(':timestamp' => $interval))->fetchField();
$output .= "$guest_count visitor(s)";
$output .= ' since ' . date('H:i d.m', $interval) /*. ' (' . date('H:i d.m', $now) . ')' . " ($interval)"*/;
$sData = array();
$gData = array();
if ($guest_count && $max_users) {
$items = db_query_range('SELECT s.hostname, MAX(s.timestamp) AS max_timestamp FROM {accesslog} s WHERE s.timestamp >= :interval GROUP BY s.hostname ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();
foreach ($items as $item) {
$ip = $item->hostname;
$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;
//$item['spider'] = 1;
break;
}
}
if ($spider == 1) {
$sData[]=array('ip' => $ip, 'host' => $host, 'timestamp' => $item->max_timestamp);
} else {
$gData[]=array('ip'=>$ip, 'host'=>$host, 'timestamp' => $item->max_timestamp);
}
}
}
if (isset($gData) && count($gData)>0){
// $output .= "<h3>Guests</h3>";
$output .= "<ul>";
foreach ($gData as $d){
$output .= "<li>" . check_plain($d['host']) . ', ' . check_plain($d['ip']);
$loc = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$d['ip&#039;]));
if ($loc['geoplugin_city'].$loc['geoplugin_region']!=''){
$output .= ", " . check_plain($loc['geoplugin_city']) . ", " . check_plain($loc['geoplugin_region']);
}
if ($loc['geoplugin_countryName'] != ''){
$output .= ", " . check_plain($loc['geoplugin_countryName']);
}
$output .= ', ' . date('H:i', $d['timestamp']);
$output .= "</li>";
}
$output .= "</ul>";
}
if (isset($sData) && count($sData)>0){
$output .= "Spiders:";
$output .= "<ul>";
foreach ($sData as $d) {
$output .= "<li>" . check_plain($d['host']) . ", " . check_plain($d['ip']) . ', ' . date('H:i', $d['timestamp']) . "</li>";
}
$output .= "</ul>";
}
$output .= " </div>";
return $output;
}
?>