how to detect search engine bots with php?

terrific picture terrific · Mar 24, 2009 · Viewed 126.2k times · Source

How can one detect the search engine bots using php?

Answer

minnur picture minnur · Feb 24, 2013

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