PHP function to detect spider bots

This php function helps to determine whether page is accessed by spider bot or not. If page is accessed by any of the bot listed in $bots_list array then function will return bot name otherwise it will return false. I have added only top 3 spider bots in list but you can expend it.

Usage examples:

  • Get email notification when your site is crawled by bots.
  • Save bots access information in database and generate custom stats.
  • Use to create special page for spider bots.
<?php
function botDetect()
{
$bots_list=array(
"Google"=>"Googlebot",
"Yahoo"=>"Slurp",
"Bing"=>"bingbot"
/*You can add more bot here*/
);
$regexp='/'.  implode("|", $bots_list).'/';
$ua=$_SERVER['HTTP_USER_AGENT'];
if(preg_match($regexp, $ua,$matches))
{
$bot=  array_search($matches[0], $bots_list);
return $bot;
}
else
{
return false;
}
}
?>

Published by Sunny

A Mechanical engineer by education and web developer by passion and profession. He lives in Lucknow, India. Currently earning his livings by hitting keyboad. ;)

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *