欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结

程序员文章站 2022-05-13 21:35:24
1、推荐的一种方法:php判断搜索引擎蜘蛛爬虫还是人为访问代码,摘自discuz x3.2

1、推荐的一种方法:php判断搜索引擎蜘蛛爬虫还是人为访问代码,摘自discuz x3.2

<?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 '人';
}
?>

实际应用中可以这样判断,直接不是搜索引擎才执行操作

<?php
if(!checkrobot()){
//do something
}
?>

2、第二种方法:

使用php实现蜘蛛访问日志统计

$useragent = addslashes(strtolower($_server['http_user_agent']));

 if (strpos($useragent, 'googlebot')!== false){$bot = 'google';}
 elseif (strpos($useragent,'mediapartners-google') !== false){$bot = 'google adsense';}
 elseif (strpos($useragent,'baiduspider') !== false){$bot = 'baidu';}
 elseif (strpos($useragent,'sogou spider') !== false){$bot = 'sogou';}
 elseif (strpos($useragent,'sogou web') !== false){$bot = 'sogou web';}
 elseif (strpos($useragent,'sosospider') !== false){$bot = 'soso';}
 elseif (strpos($useragent,'360spider') !== false){$bot = '360spider';}
 elseif (strpos($useragent,'yahoo') !== false){$bot = 'yahoo';}
 elseif (strpos($useragent,'msn') !== false){$bot = 'msn';}
 elseif (strpos($useragent,'msnbot') !== false){$bot = 'msnbot';}
 elseif (strpos($useragent,'sohu') !== false){$bot = 'sohu';}
 elseif (strpos($useragent,'yodaobot') !== false){$bot = 'yodao';}
 elseif (strpos($useragent,'twiceler') !== false){$bot = 'twiceler';}
 elseif (strpos($useragent,'ia_archiver') !== false){$bot = 'alexa_';}
 elseif (strpos($useragent,'iaarchiver') !== false){$bot = 'alexa';}
 elseif (strpos($useragent,'slurp') !== false){$bot = '雅虎';}
 elseif (strpos($useragent,'bot') !== false){$bot = '其它蜘蛛';}
 if(isset($bot)){
   $fp = @fopen('bot.txt','a');
   fwrite($fp,date('y-m-d h:i:s')."\t".$_server["remote_addr"]."\t".$bot."\t".'http://'.$_server['server_name'].$_server["request_uri"]."\r\n");
   fclose($fp);
 }

第三种方法:

我们可以通过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; 
}

下面的php代码附带了更多的蜘蛛标识

function iscrawler() { 
    echo $agent= strtolower($_server['http_user_agent']); 
    if (!empty($agent)) { 
        $spidersite= array( 
            "tencenttraveler", 
            "baiduspider+", 
            "baidugame", 
            "googlebot", 
            "msnbot", 
            "sosospider+", 
            "sogou web spider", 
            "ia_archiver", 
            "yahoo! slurp", 
            "youdaobot", 
            "yahoo slurp", 
            "msnbot", 
            "java (often spam bot)", 
            "baiduspider", 
            "voila", 
            "yandex bot", 
            "bspider", 
            "twiceler", 
            "sogou spider", 
            "speedy spider", 
            "google adsense", 
            "heritrix", 
            "python-urllib", 
            "alexa (ia archiver)", 
            "ask", 
            "exabot", 
            "custo", 
            "outfoxbot/yodaobot", 
            "yacy", 
            "surveybot", 
            "legs", 
            "lwp-trivial", 
            "nutch", 
            "stackrambler", 
            "the web archive (ia archiver)", 
            "perl tool", 
            "mj12bot", 
            "netcraft", 
            "msiecrawler", 
            "wget tools", 
            "larbin", 
            "fish search", 
        ); 
        foreach($spidersite as $val) { 
            $str = strtolower($val); 
            if (strpos($agent, $str) !== false) { 
                return true; 
            } 
        } 
    } else { 
        return false; 
    } 
} 
if (iscrawler()){ 
    echo "你好蜘蛛精!"; 
} 
else{ 
   echo "你不是蜘蛛精啊!"; 
} 

第四种方法:

<?php
$flag = false;
$tmp = $_server['http_user_agent'];
if(strpos($tmp, 'googlebot') !== false){
  $flag = true;
} else if(strpos($tmp, 'baiduspider') >0){
  $flag = true;
} else if(strpos($tmp, 'yahoo! slurp') !== false){
  $flag = true;
} else if(strpos($tmp, 'msnbot') !== false){
  $flag = true;
} else if(strpos($tmp, 'sosospider') !== false){
  $flag = true;
} else if(strpos($tmp, 'yodaobot') !== false || strpos($tmp, 'outfoxbot') !== false){
  $flag = true;
} else if(strpos($tmp, 'sogou web spider') !== false || strpos($tmp, 'sogou orion spider') !== false){
  $flag = true;
} else if(strpos($tmp, 'fast-webcrawler') !== false){
  $flag = true;
} else if(strpos($tmp, 'gaisbot') !== false){
  $flag = true;
} else if(strpos($tmp, 'ia_archiver') !== false){
  $flag = true;
} else if(strpos($tmp, 'altavista') !== false){
  $flag = true;
} else if(strpos($tmp, 'lycos_spider') !== false){
  $flag = true;
} else if(strpos($tmp, 'inktomi slurp') !== false){
  $flag = true;
}
if($flag == false){
  header("location: //www.jb51.net" . $_server['request_uri']);
  // 自动转到//www.jb51.net 对应的网页
  // $_server['request_uri'] 为域名后面的路径
  // 或换成header("location: //www.jb51.net/abc/d.php");
  exit();
}
?>