Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Parse Out Bot's By User Agent (http://www.go4expert.com/forums/parse-bots-user-agent-t17986/)

codytaylor 14Jun2009 12:30

Parse Out Bot's By User Agent
 
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 14Jun2009 16:26

Re: Parse Out Bot's By User Agent
 
Nice little utility

LenoxFinlay 28Jul2009 11:28

Re: Parse Out Bot's By User Agent
 
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.


All times are GMT +5.5. The time now is 00:24.