php判断搜索引擎蜘蛛爬虫的方法整理

六月 28, 2019 | views
Comments 0

先来看蜘蛛列表:

搜索引擎 user-agent(包含) 是否PTR 备注

google Googlebot host ip  得到域名:googlebot.com主域名

baidu Baiduspider host ip  得到域名:*.baidu.com 或 *.baidu.jp

yahoo Yahoo! host ip  得到域名:inktomisearch.com主域名

Sogou Sogou ×

*Sogou web spider/3.0(+http://www.sogou.com/docs/help/webmasters.htm#07″)

*Sogou Push Spider/3.0(+http://www.sogou.com/docs/help/webmasters.htm#07″)

网易 YodaoBot × *Mozilla/5.0 (compatible; YodaoBot/1.0;http://www.yodao.com/help/webmaster/spider/”; )

MSN MSNBot host ip  得到域名:live.com主域名

360 360Spider × Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.0.11)  Firefox/1.5.0.11; 360Spider

soso Sosospider × Sosospider+(+http://help.soso.com/webspider.htm)

bing bingbot host ip  得到域名:msn.com主域名

再来看看例子:

  1. <?php 
  2. //php判断搜索引擎蜘蛛爬虫的方法 
  3.  
  4. function checkrobot($useragent='') { 
  5.     static $kw_spiders = array('bot''crawl''spider''slurp''sohu-search''lycos''robozilla'); 
  6.     static $kw_browsers = array('msie''netscape''opera''konqueror''mozilla'); 
  7.  
  8.     $useragent = strtolower(emptyempty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent); 
  9.     if (strpos($useragent'http://') === false && dstrpos($useragent$kw_browsers)) 
  10.         return false; 
  11.     if (dstrpos($useragent$kw_spiders)) 
  12.         return true; 
  13.     return false; 
  14.  
  15. function dstrpos($string$arr$returnvalue = false) { 
  16.     if (emptyempty($string)) 
  17.         return false; 
  18.     foreach ((array$arr as $v) { 
  19.         if (strpos($string$v) !== false) { 
  20.             $return = $returnvalue ? $v : true; 
  21.             return $return
  22.         } //phpfensi.com 
  23.     } 
  24.     return false; 
  25.  
  26. if(checkrobot()){ 
  27.     echo '蜘蛛'
  28. }else
  29.     echo '人类'
  30.  
  31. ?> 

PHP反解析IP方法,例子:


 
  1. /** 
  2.  *检查IP及蜘蛛真实性 
  3.  * (check_spider('66.249.74.44',$_SERVER['HTTP_USER_AGENT'])); 
  4.  * @copyright  http://blog.chacuo.net 
  5.  * @author 8292669 
  6.  * @param string $ip IP地址 
  7.  * @param string $ua ua地址 
  8.  * @return false|spidername  false检测失败不在指定列表中 
  9.  */ 
  10. function check_spider($ip,$ua
  11.  static $spider_list=array
  12.  'google'=>array('Googlebot','googlebot.com'), 
  13.  'baidu'=>array('Baiduspider','.baidu.'), 
  14.  'yahoo'=>array('Yahoo!','inktomisearch.com'), 
  15.  'msn'=>array('MSNBot','live.com'), 
  16.  'bing'=>array('bingbot','msn.com'
  17.  ); 
  18.   
  19.  if(!preg_match('/^(\d{1,3}\.){3}\d{1,3}$/',$ip)) return false; 
  20.  if(emptyempty($ua)) return false; 
  21.   
  22.  foreach ($spider_list as $k=>$v
  23.  { 
  24.   ///如果找到了 
  25.   if(stripos($ua,$v[0])!==false) 
  26.   { 
  27.    $domain = gethostbyaddr($ip); 
  28.  
  29.    if($domain && stripos($domain,$v[1])!==false) 
  30.    { 
  31.     return $k
  32.    } //phpfensi.com 
  33.   } 
  34.  } 
  35.  return false; 

目前只加入几个搜索引擎检测,这些是可以做反解析查询的。不能做反解析查询的,最好做速度限制,用户会使用它们来伪造搜索引擎来抓取你的资源。



zend