当前位置: 移动技术网 > IT编程>开发语言>PHP > php 判断访客是否为搜索引擎蜘蛛的函数代码

php 判断访客是否为搜索引擎蜘蛛的函数代码

2019年04月21日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

/**
* 判断是否为搜索引擎蜘蛛
*
* @author eddy
* @return bool
*/
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;
}
}


网上倒是能搜到一大把,不过都是千篇一律的复制来复制去的,也不够全面,我这里整理了一份比较全面的代码:

复制代码 代码如下:

function is_spider(){
$robot = 0;
$user_agent = strtolower($_server['http_user_agent']);
if(strpos($user_agent,"bot")) $robot = 1;
if(strpos($user_agent,"spider")) $robot = 1;
if(strpos($user_agent,"slurp")) $robot = 1;
if(strpos($user_agent,"mediapartners-google")) $robot = 1;
if(strpos($user_agent,"fast-webcrawler")) $robot = 1;
if(strpos($user_agent,"altavista")) $robot = 1;
if(strpos($user_agent,"ia_archiver")) $robot = 1;
if($robot == 1){
//do something
}
return '';
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网