No replies
bradjmsu's picture
Offline
Joined: 03/25/2011
Juice: 14
Was this information Helpful?

I just ported and extended a Drupal 6 module to Drupal 7 for seeing who's online (including guests). Its really not Ubercart specific, but it is great to see who's online shopping in your store or what Spiders are on your site at any given time. Anyway, full implementation instructions can be found at Drupal 7 Who's Online Module-Extended. If you would just like the code, this is what I've got:

<?php
$number
= db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1')->fetchField();
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_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= :time AND uid = 0', array(':time'=> time() - $time_period))->fetchObject();//removed db_fetch_object
 
$guests_hostname = db_query('SELECT hostname FROM {sessions} WHERE timestamp >= :time AND uid = 0', array(':time'=> time() - $time_period));
 
$total_guests = db_query('SELECT COUNT(hostname) FROM {sessions} WHERE timestamp >= :time AND uid = 0', array(':time'=> time() - $time_period))->fetchField();
 
 
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= :time AND uid != 0 ORDER BY access DESC', array(':time'=> time() - $time_period));lp;olp;
 
$total_users = db_query('SELECT COUNT(uid), name, access FROM {users} WHERE access >= :time AND uid != 0 ORDER BY access DESC', array(':time'=> time() - $time_period))->fetchField();

 

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

 

// Display a list of currently online guests.
 
if ($total_guests) {
   
$output.="<div class=\"item-list\"><h3>Guests</h3><ul><fine>";
   
$guestitems = array();
    foreach(
$guests_hostname as $account){ //removed db_fetch_object
     
if (!$guests--) break;
     
$guestitems[] = $account->hostname;
     
$textHostname=gethostbyaddr($account->hostname);
     
$spiders=array('google', 'fatlens', 'yahoo', 'altavista', 'yandex', 'baidu', 'bing', 'thefind', 'bot', 'spider', 'crawl');
     
$spider=0;
      foreach (
$spiders as $spider){
          if (
strpos($textHostname, $spider)!==false){
             
$spiderOutput[]=$textHostname;
             
$spider=1;
              break;
          }
      }
      if (
$spider==0){
         
$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>";
  }
}
if (
count($spiderOutput)>0){
   
$output.="<div class=\"item-list\"><h3>Spiders</h3><ul><fine>";
    foreach (
$spiderOutput as $spider){
       
$output.="<li><a title=\"Go to address\" href=\"http://$spider\">$spider</a>
          <a title=\"Go to address\" href=\"http://"
.$spider."\">".$spider."</a>       ";
    }
}
return
$output;
?>