零基础建站培训介绍

当前位置:

PHP判断控制搜索引擎蜘蛛与普通用户显示内容不一样

PHP判断控制搜索引擎蜘蛛与普通用户看到的内容不一样。

如果只想判断百度蜘蛛,就可以使用以下的代码:


if (ereg("http://www.baidu.com/search/spider.htm", $_SERVER["HTTP_USER_AGENT"])){
$file = file_get_contents('https://www.xuewangzhan.net/');
echo $file;
exit;
}

推荐的一种方法:php判断搜索引擎蜘蛛爬虫还是人为访问代码。


<?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来判断是否是蜘蛛,搜索引擎的蜘蛛都有自己的独特标志。


<?php
function isCrawler() {
$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()){
?>
是搜索引擎蜘蛛
<?php }else{ ?>
不是搜索引擎蜘蛛
<?php } ?>

第三种方法:


<?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: https://www.xuewangzhan.net" . $_SERVER['REQUEST_URI']);
// 自动转到https://www.xuewangzhan.net 对应的网页
// $_SERVER['REQUEST_URI'] 为域名后面的路径
// 或换成header("Location: https://www.xuewangzhan.net/abc/d.php");
exit();
}
?>
上一课: 下一课:

发表评论

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