1. 问答
  2. 问答详情

PHP判断来访是搜索引擎蜘蛛还是普通用户

PHP判断来访是搜索引擎蜘蛛还是普通用户

1个回答

0

采纳

php判断搜索引擎蜘蛛爬虫还是普通用户

function checkrobot($useragent=''){
   static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
   static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');

   $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
   if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
   if(dstrpos($useragent, $kw_spiders)) return true;
   return false;
}
function dstrpos($string, $arr, $returnvalue = false) {
   if(empty($string)) return false;
   foreach((array)$arr as $v) {
       if(strpos($string, $v) !== false) {
           $return = $returnvalue ? $v : true;
           return $return;
       }
   }
   return false;
}
if(checkrobot()){
   echo '搜索引擎';
}else{
   echo '普通用户';
}



HTTP_USER_AGENT

判断是否是蜘蛛,搜索引擎的蜘蛛都有自己的独特标志。

function is_crawler() {
   $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
   $spiders = array(
       'Googlebot', // Google 爬虫
       'Baiduspider', // 百度爬虫
       'Yahoo! Slurp', // 雅虎爬虫
       'YodaoBot', // 有道爬虫
       'msnbot' // Bing爬虫
        // 更多爬虫关键字
   );
   foreach ($spiders as $spider) {
       $spider = strtolower($spider);
       if (strpos($userAgent, $spider) !== false) {
           return true;
       }
   }
   return false;
}


撰写答案

验证码
点击刷新