2 March 2007

Ignore spiders in page count ...

So I had this page count that incremented every time a user visited the page. This was artificially inflated with spiders/bots… sure the client likes to see bigger numbers … but wanted it a little more real…

//grab user agent
$user_agent = (!empty( $_SERVER['HTTP_USER_AGENT'])) ? 
$_SERVER['HTTP_USER_AGENT'] : "";        

//check for crawler
$is_a_crawler = preg_match("/(?:bot\b|spider|slurp|crawler|
ultraseek|webcopier|\bwget\b|asterias)/i",$user_agent);

if($user_agent && !$is_a_crawler) 
{
	//SQL (pseudo code)
	//-----------------
	//UPDATE _Table_ 
	//SET count=count+1 
	//WHERE ID=ID-OF-PAGE
}

↵ denotes wrapping

The very simple explanation of the PHP...

  1. grab user agent
  2. check to see if agent is "spiderlike"
  3. increment count variable

It's probably not perfect ... but will you closer to TRUER numbers.

Source file: pagecount-nospider.txt


 

comment



note: you can only submit after you hit preview


nuff-respec is a weblog written by daniel bulli a senior web programmer in boston, ma.
more >

contact | resume | profile | twitter

recently :::