SoFunction
Updated on 2025-03-06

PHP implements a method to determine whether the access is a search engine robot

This article describes the method of php to determine whether the access origin is a search engine robot. Share it for your reference. The specific analysis is as follows:

Many times we need to identify the source of website visitors and make different actions for real users and search engines. Then we need to first determine whether it is a search engine.

The php judgment method is very simple. You can identify it by filtering the $_SERVER['HTTP_USER_AGENT'] parameter. The following is the relevant source code of an excerpt from an open source program:

private function getRobot()
{
 if (empty($_SERVER['HTTP_USER_AGENT']))
 {
  return false;
 }
 $searchEngineBot = array(
  'googlebot'=>'google',
  'mediapartners-google'=>'google',
  'baiduspider'=>'baidu',
  'msnbot'=>'msn',
  'yodaobot'=>'yodao',
  'youdaobot'=>'yodao',
  'yahoo! slurp'=>'yahoo',
  'yahoo! slurp china'=>'yahoo',
  'iaskspider'=>'iask',
  'sogou web spider'=>'sogou',
  'sogou push spider'=>'sogou',
  'sosospider'=>'soso',
  'spider'=>'other',
  'crawler'=>'other',
 );
 $spider = strtolower($_SERVER['HTTP_USER_AGENT']);
 foreach ($searchEngineBot as $key => $value)
 { 
  if (strpos($spider, $key)!== false)
  {
   return $value;
  }
 }
 return false;
}
public function isRobot()
{
 if($this->getRobot()!==false)
 {
  return true;
 }
 return false;
}

I hope this article will be helpful to everyone's PHP programming.