Home  >  Article  >  Backend Development  >  php实现判断访问来路是否为搜索引擎机器人的方法_php技巧

php实现判断访问来路是否为搜索引擎机器人的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:17:321070browse

本文实例讲述了php实现判断访问来路是否为搜索引擎机器人的方法。分享给大家供大家参考。具体分析如下:

很多时候我们需要对网站访客来路进行识别,针对真实用户与搜索引擎作不同动作实现,那么首先就需要判断是否为搜索引擎。

php判断方法非常简单,通过过滤$_SERVER['HTTP_USER_AGENT'] 参数即可进行识别,以下是摘录某开源程序的相关源码:

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;
}

希望本文所述对大家的php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn