Are you new? Read the FAQ and catch up on!
4k views

How to detect if a page is visited by a bot

Using the HTTP_USER_AGENT, it is possible to know if your page is visited by a bot. This is useful if you don't want to count these visits in your visits counter:

PHP
  1. protected function isBot()
  2. {
  3.     /* This function will check whether the visitor is a search engine robot */
  4.  
  5.     $bots = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
  6.     "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
  7.     "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
  8.     "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
  9.     "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
  10.     "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
  11.     "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
  12.     "Butterfly","Twitturls","Me.dium","Twiceler");
  13.  
  14.     foreach($bots as $bot)
  15.     {
  16.             if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
  17.                 return true;
  18.     }
  19.  
  20.     return false;
  21. }
  22.  

­

Tags: PHP user_agent bot

Embed: