How to detect search engine bots with PHP?

A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).

Example

if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) {
   //other steps that need to be used
}

The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.

Example

function _bot_detected() {
   return (
      isset($_SERVER['HTTP_USER_AGENT'])
      && preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT']);
}

The preg_match function helps in finding specific patterns in the string. To the preg_match function, the bot name is passed and it is compared with the user agent that detects if the spider is a search engine bot or not.

kabeer

Added  Jul 01, 02:47 am

Article tags

×
Google login
SignUp with Email
X