冰豆网

分享网络精彩
bingdou.com.cn

PHP判断UA 给搜索蜘蛛和用户呈现不同的页面

时间:2023-02-23加入收藏

原理:判断UA是搜索蜘蛛则让他们看到所有的页面代码。如果是真实用户,则跳转到其他页面
这种方法能达到想要的目的,又可以做好seo,简直完美。
<?php
$http = false;
$http1 = $_SERVER['HTTP_USER_AGENT'];
if(strpos($http1, 'Googlebot') !== false){
$http = true;
} else if(strpos($http1, 'Baiduspider') >0){
$http = true;
} else if(strpos($http1, 'Yahoo! Slurp') !== false){
$http = true;
} else if(strpos($http1, 'msnbot') !== false){
$http = true;
} else if(strpos($http1, 'Sosospider') !== false){
$http = true;
} else if(strpos($http1, 'YodaoBot') !== false || strpos($http1, 'OutfoxBot') !== false){
$http = true;
} else if(strpos($http1, 'Sogou web spider') !== false || strpos($http1, 'Sogou Orion spider') !== false){
$http = true;
} else if(strpos($http1, 'fast-webcrawler') !== false){
$http = true;
} else if(strpos($http1, 'Gaisbot') !== false){
$http = true;
} else if(strpos($http1, 'ia_archiver') !== false){ 
$http = true;
} else if(strpos($http1, 'altavista') !== false){
$http = true;
} else if(strpos($http1, 'lycos_spider') !== false){
$http = true;
} else if(strpos($http1, 'Inktomi slurp') !== false){
$http = true;
} else if(strpos($http1, 'googlebot-mobile') !== false){
$http = true;
} else if(strpos($http1, '360Spider ') !== false){
$http = true;
} else if(strpos($http1, 'haosouspider') !== false){
$http = true;
} else if(strpos($http1, 'Sogou News Spider') !== false){
$http = true;
} else if(strpos($http1, 'YoudaoBot') !== false){
$http = true;
} else if(strpos($http1, 'bingbot') !== false){
$http = true;
} else if(strpos($http1, 'YisouSpider') !== false){
$http = true;
} else if(strpos($http1, 'ia_archiver') !== false){
$http = true;
} else if(strpos($http1, 'EasouSpider') !== false){
$http = true;
} else if(strpos($http1, 'JikeSpider') !== false){
$http = true;
} else if(strpos($http1, 'Sogou blog') !== false){
$http = true;
}
if($http == false){
header("Location: https://www.bingdou.com.cn/");
exit();
}
?>
上面的代码包含了各大搜索引擎的蜘蛛,你可以把 https://ww.bingdou.com.cn 替换成你自己想要的网址
代码可以放在index.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;
} else if(strpos($tmp, 'googlebot-mobile') !== false){
$flag = true;
} else if(strpos($tmp, '360Spider ') !== false){
$flag = true;
} else if(strpos($tmp, 'haosouspider') !== false){
$flag = true;
} else if(strpos($tmp, 'Sogou News Spider') !== false){
$flag = true;
} else if(strpos($tmp, 'YoudaoBot') !== false){
$flag = true;
} else if(strpos($tmp, 'bingbot') !== false){
$flag = true;
} else if(strpos($tmp, 'YisouSpider') !== false){
$flag = true;
} else if(strpos($tmp, 'ia_archiver') !== false){
$flag = true;
} else if(strpos($tmp, 'EasouSpider') !== false){
$flag = true;
} else if(strpos($tmp, 'JikeSpider') !== false){
$flag = true;
} else if(strpos($tmp, 'Sogou blog') !== false){
$flag = true;
}
if($flag == false){
header('HTTP/1.1 200'); //状态码便于分析网页是否ok
$url='502.php'; //根目录随便的文件
$html= file_get_contents($url);
echo$html;//输出你展示的502.php内容
exit();//不输出泛目录内容
}

打 赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

TGA: php 技巧

分享到:


官方微信二维码冰豆网官方微信公众号