[PHP]根据User Agent判断是否搜索引擎蜘蛛

Leave a comment
/* 判断搜索引擎 摘自Discuz x3.2 */
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;
}

/* 判断搜索引擎 */
function checkrobot() { 
  $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; 
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注