Discuz教程 判断搜索引擎蜘蛛访问

Discuz系统中原生集中了判断搜索引擎蜘蛛爬虫访问的函数checkrobot(),方法定义在文件:\source\function\function_core.php
<ignore_js_op>
checkrobot()具体代码如下:

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;
}

函数功能说明:
checkrobot()函数声明了一些常见的蜘蛛爬虫UserAgent,分析全局变量$_SERVER中的$_SERVER『'HTTP_USER_AGENT]根据$useragent关键词判断该访问是否是搜索引擎蜘蛛爬虫。

由于只是爬虫关键词匹配,可能会有一定的误判,有兴趣的DZ站长可以对此函数进行优化。

使用方法举例:
if(checkrobot()){
//访客是蜘蛛爬虫的情况,执行的流程
}
else{
//访客是自然访客情况下的执行流程
}

来源:我爱自学Discuz教程搜索引擎蜘蛛

posted @ 2022-01-03 19:04  is_jackwen  阅读(164)  评论(0编辑  收藏  举报

更多内容,请访问我爱自学网