Parse Out Bot's By User Agent

codytaylor's Avatar, Join Date: Jun 2009
Newbie Member
Useful function for logging stats and visitors in php.
PHP Code:
//returns 1 if the user agent is a bot
function is_bot($user_agent)
{
  
//if no user agent is supplied then assume it's a bot
  
if($user_agent == "")
    return 
1;

  
//array of bot strings to check for
  
$bot_strings = Array(  "google",     "bot",
            
"yahoo",     "spider",
            
"archiver",   "curl",
            
"python",     "nambu",
            
"twitt",     "perl",
            
"sphere",     "PEAR",
            
"java",     "wordpress",
            
"radian",     "crawl",
            
"yandex",     "eventbox",
            
"monitor",   "mechanize",
            
"facebookexternal"
          
);
  foreach(
$bot_strings as $bot)
  {
    if(
strpos($user_agent,$bot) !== false)
    { return 
1; }
  }
  
  return 
0;

shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nice little utility
LenoxFinlay's Avatar
Banned
I insert at the very end of the page after I flush everything out so there will be no delay of content even if the db is bogged down. And if I was give it to someone else who is using iis I would have to recode the entire thing. Also when doing reporting on large sets of data I don’t want to have to parse huge log files for every report.