how to detect search engine bots with php?


Question

How can one detect the search engine bots using php?

1
113
3/31/2015 5:38:07 AM

Accepted Answer

Here's a Search Engine Directory of Spider names

Then you use $_SERVER['HTTP_USER_AGENT']; to check if the agent is said spider.

if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
    // what to do
}
71
3/24/2009 1:57:15 PM

I use the following code which seems to be working fine:

function _bot_detected() {

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

update 16-06-2017 https://support.google.com/webmasters/answer/1061943?hl=en

added mediapartners


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon