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

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

程序员文章站 2023-03-25 08:18:26
复制代码 代码如下: /** * 判断是否为搜索引擎蜘蛛 * * @author eddy * @return bool */ function iscrawler() {...
复制代码 代码如下:

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