All pastes #42834 Raw Edit

Someone

public text v1 · immutable
#42834 ·published 2006-02-22 23:20 UTC
rendered paste body
<?php 
/* 
  $Page ID: visitor_count.php,v2.2d, Dated: 14 August 2005 [Visitors File] Ian-San $
  http://www.gowebtools.com

  osCommerce, Open Source E-Commerce Solutions 
  http://www.oscommerce.com 

  Copyright (c) 2002 osCommerce 

  Released under the GNU General Public License 
*/ 

// Change following setting if you wish
$ip_exclude = '00.00.00.00'; // If you wish to exclude an ip address, enter it here
$trace_users = true; // Set to false if you do not want to trace visitors
$trace_spiders = false; // Set to true if you do want to trace spiders
$visitors_spiders = true; // Set to false if you do want to include hits from spiders in any Stats Table (takes precedence over $trace_spiders)
// Change above setting if you wish

// Dont change anything below this line
$b_lang = getenv('HTTP_ACCEPT_LANGUAGE');
$referer = getenv('HTTP_REFERER');
if ($referer == '') {
  $referer = $HTTP_SERVER_VARS['HTTP_REFERER'];
}
if ($referer == '') {
  $referer = 'direct';
}

$uri = sprintf("%s%s%s","http://",$HTTP_HOST,$REQUEST_URI);
if ($uri == '') {
  $uri = getenv('HTTP_REFERER');
}

if ( ($uri == '')) {
  $uri = '/';
}

if ( function_exists('tep_get_ip_address') ) {
  $b_ip = tep_get_ip_address();
} else {
  $b_ip = getenv('REMOTE_ADDR');
}

$unwanted = stristr($b_ip,",");
if ($unwanted !== false) {
  $b_ip = str_replace($unwanted,"",$b_ip);
}
$b_ip = substr($b_ip,0,15);

$hua = getenv('HTTP_USER_AGENT');
$bots=array('gigablast', 'Googlebot', 'Yahoo! Slurp', 'Slurp', 'ZyBorg', 'MSNbot', 'ia_archiver', 'Zao-Crawler', 'SurveyBot', 'NutchCVS', 'WorQmada', 'Faxobot', 'Konqueror', 'W3C_Validator', 'BorderManager', 'contype', 'Ask Jeeves/Teoma', 'Windows-Media-Player', 'TygoProwler', 'exactseek-pagereaper', 'Netcraft Web Server Survey', 'arameda.com Spider', 'Indy Library', 'MJ12bot');
foreach ($bots as $needle){
         $search = stristr($hua, $needle);
         if ($search != false) $b_lang = $needle;
}

switch($b_lang){
   case 'gigablast':
         $b_lang = '[Gigablast]';
   break;
   case 'Googlebot':
         $b_lang = '[Google]';
   break;
   case 'Yahoo! Slurp':
      $b_lang = '[Yahoo!]';
   break;
   case 'Slurp':
      $b_lang = '[Inktomi]';
   break;
   case 'ZyBorg':
      $b_lang = '[Wisenut]';
   break;
   case 'MSNbot':
      $b_lang = '[MSN]';
   break;
   case 'ia_archiver':
      $b_lang = '[Alexa]';
   break;
   case 'Zao-Crawler':
      $b_lang = '[Zao-Crawler]';
   break;
   case 'SurveyBot':
      $b_lang = '[whois.sc]';
   break;
   case 'NutchCVS':
      $b_lang = '[Nutch.org]';
   break;
   case 'WorQmada':
      $b_lang = '[WorQmada]';
   break;
   case 'Faxobot':
      $b_lang = '[Faxobot fax harvestor]';
   break;
   case 'Konqueror':
      $b_lang = '[Konqueror web browser]';
   break;
   case 'W3C_Validator':
      $b_lang = '[W3C]';
   break;
   case 'BorderManager':
      $b_lang = '[Novel BorderManager]';
   break;
   case 'contype':
      $b_lang = '[contype]';
   break;
   case 'Ask Jeeves/Teoma':
      $b_lang = '[Ask Jeeves/Teoma]';
   break;
   case 'Windows-Media-Player':
      $b_lang = '[Windows-Media-Player]';
   break;
   case 'TygoProwler':
      $b_lang = '[Tygo.com]';
   break;
   case 'exactseek-pagereaper':
      $b_lang = '[ExactSeek]';
   break;
   case 'Netcraft Web Server Survey':
      $b_lang = '[Netcraft Web Server Survey]';
   break;
   case 'arameda.com Spider':
      $b_lang = '[Arameda.com]';
   break;
   case 'Indy Library':
      $b_lang = '[Indy Library Email harvester]';
   break;
   case 'MJ12bot':
      $b_lang = '[majestic12.co.uk]';
   break;
}

if ( ($b_lang == '')) {
  $b_lang = '[Unknown]';
}

$b_lang = substr($b_lang,0,32);

if (substr($b_lang,0,1) != '[') {
  $spider_ip = array("216.239.46.", "64.62.168.", "64.209.181.53", "64.208.33.33", "64.209.181.52", "209.185.108.", "209.185.253", "216.239.49.", "204.123.", "204.74.103.", "203.108.10.", "195.4.183.", "195.242.46.", "198.3.97.", "204.62.245.", "193.189.227.", "209.1.12.", "204.162.96.", "204.162.98.", "194.121.108.", "128.182.72.", "207.77.91.", "206.79.171.", "207.77.90.", "208.213.76.", "194.124.202.", "193.114.89.", "193.131.74.", "131.84.1.", "208.219.77.", "206.64.113.", "195.186.1.", "195.3.97.", "194.191.121.", "139.175.250.", "209.73.233.", "194.191.121.", "198.49.220.", "204.62.245.", "198.3.99.", "198.2.101.", "204.192.112.", "206.181.238", "208.215.47.", "171.64.75.", "204.162.98.", "204.162.96.", "204.123.9.52", "204.123.2.44", "204.74.103.39", "204.123.9.53", "204.62.245.", "206.64.113.", "204.138.115.", "94.22.130.", "164.195.64.1", "205.181.75.169", "129.170.24.57", "204.162.96.", "204.162.96.", "204.162.98.", "204.162.96.", "207.77.90.", "207.77.91.", "208.200.146.", "204.123.9.20", "204.138.115.", "209.1.32.", "209.1.12.", "192.216.46.49", "192.216.46.31", "192.216.46.30", "203.9.252.2"); 
  $i = 0; 
  while ($i < (count($spider_ip))) { 
    if (strstr($b_ip, $spider_ip[$i])) { 
      $b_lang = '[Spider IP]';
      break; 
    } 
    $i++; 
  } 
} 

if ( ($b_ip != $ip_exclude) && ($visitors_spiders || (!$visitors_spiders && (substr($b_lang,0,1) != '[') )) ) {
  if ($customer_id > 0) { // 1. We know who customer is
  
// Add Customer entry to Visitors
    if (!tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"))) {

// A1. No entries exist so add new Customer Entry to Visitors
      tep_db_query("insert into " . TABLE_VISITORS . " (date,customers_id,counter,browser_ip,browser_language,online,language,referer,uri) values (now(),'" . (int)$customer_id . "','1','".$b_ip."','".$b_lang."',now(),'".$language."','".tep_db_input($referer)."','".tep_db_input($uri)."')");

// A2. Add and update Customer entries in Visitors Trace
      if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){
        tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')");
        tep_db_query("update " . TABLE_VISITORS_TRACE . " set customers_id = '" . (int)$customer_id . "' where browser_ip = '" . $b_ip . "' AND to_days(now()) - to_days(date) < 1");
      }

    } else {

// B1. Either one or two entries exist
    if (tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1")) == '1') {

// B1a. Only one Customer entry so just update it in Visitors
      tep_db_query("update " . TABLE_VISITORS . " set counter = counter+1, customers_id = '" . (int)$customer_id . "', online = now(), browser_ip = '" . $b_ip . "' where (customers_id = '" . (int)$customer_id . "' || browser_ip = '" . $b_ip . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1");
	
	} else {

// B1b. Must be two Customer entries so need to delete newest and update oldest in Visitors
      $counter_old_raw = tep_db_query("select browser_id, counter FROM " . TABLE_VISITORS . " where (browser_ip = '" . $b_ip . "' || customers_id = '" . (int)$customer_id . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1 order by browser_id DESC limit 1");
	  $counter_old = tep_db_fetch_array($counter_old_raw);
	  $counter_sum = $counter_old["counter"];
	  $browser_id = $counter_old["browser_id"];
	  
      tep_db_query("delete from " . TABLE_VISITORS . " where browser_id = '" . $browser_id . "'");
	  tep_db_query("update " . TABLE_VISITORS . " set counter = counter + '" . $counter_sum . "' + 1, customers_id = '" . (int)$customer_id . "', online = now(), browser_ip = '" . $b_ip . "' where (customers_id = '" . (int)$customer_id . "' || browser_ip = '" . $b_ip . "') AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1");
	}

// B2. Add and update Customer entries in Visitors Trace
      if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){
        tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')");
        tep_db_query("update " . TABLE_VISITORS_TRACE . " set customers_id = '" . (int)$customer_id . "', browser_ip = '" . $b_ip . "' where (browser_ip = '" . $b_ip . "' || customers_id = '" . $customer_id . "') AND to_days(now()) - to_days(date) < 1");
      }
    }

  } else { // 2. We dont know who customer is
  
// Add Anonymous entry to Visitors
  if (!tep_db_num_rows(tep_db_query("select counter FROM " . TABLE_VISITORS . " where browser_ip = '" . $b_ip . "' AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1"))) {

// A. New Anonymous entry to Visitors
    tep_db_query("insert into " . TABLE_VISITORS . " (date,counter,browser_ip,browser_language,online,language,referer,uri) values (now(),'1','".$b_ip."','".$b_lang."',now(),'".$language."','".tep_db_input($referer)."','".tep_db_input($uri)."')");
  } else {

// B. Update Anonymous entry to Visitors
    tep_db_query("update " . TABLE_VISITORS . " set counter = counter+1, online = now() where browser_ip = '" . $b_ip . "' AND browser_language = '" . $b_lang . "' AND to_days(now()) - to_days(date) < 1");
  }

// C. Add Anonymous entry to Visitors Trace
  if (((substr($b_lang, 0, 1) == '[') && $trace_spiders) || ((substr($b_lang, 0, 1) != '[') && $trace_users)){
      tep_db_query("insert into " . TABLE_VISITORS_TRACE . " (date,customers_id,browser_ip,uri) values (now(),'" . (int)$customer_id . "','".$b_ip."','".tep_db_input($uri)."')");
  }  

  }
}
?>