discuz是一个很好用,且比较出名的社区论坛程序,我经常会读一下代码。把喜欢的功能收藏起来。比如今天要介绍的检测是否为搜索引蛛蛛的代码就比较好用。
我个人将其简单的封装了一下。如果有需要的可以直接复制使用。
class is_robots{
//检查是否为蜘蛛
public static 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 (self::dstrpos($useragent, $kw_spiders)) return true;
if (strpos($useragent, 'http://') === false && self::dstrpos($useragent, $kw_browsers)) return false;
return false;
}
//检测字符串$string 是否包含数组 $arr 里的 value
public static 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;
}
}
假如你使用了Laravel框架,哪么代码就比较简单了。写一个辅助函数如下即可:
if (!function_exists('is_robot')) {
//检查是否为蜘蛛
function is_robot($useragent = '')
{
static $kw_spiders = ['bot', 'crawl', 'spider', 'slurp', 'sohu-search', 'lycos', 'robozilla'];
$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
return IlluminateSupportStr::contains($useragent, $kw_spiders);
}
}
更简洁的代码:
if (!function_exists('is_robot')) {
//检查是否为蜘蛛
function is_robot($useragent = '')
{
return Str::contains(
Str::lower($useragent ?: $_SERVER['HTTP_USER_AGENT']),
['bot', 'crawl', 'spider', 'slurp', 'sohu-search', 'lycos', 'robozilla']
);
}
}
我最终使用了下面的代码:
if (!function_exists('is_robot')) {
//检查是否为蜘蛛
function is_robot($useragent = '')
{
return str($useragent ?: $_SERVER['HTTP_USER_AGENT'])
->lower()
->contains(['bot', 'crawl', 'spider', 'slurp', 'sohu-search', 'lycos', 'robozilla']);
}
}
常见搜索引擎蜘蛛请求头:
百度Spider
Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)
“Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)”
“Baiduspider-image+(+http://www.baidu.com/search/spider.htm)”
360Spider UA 360搜索
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);
360spider (http://webscan.360.cn)
Google Spider UA
“Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)”
“Googlebot-Image/1.0”
Adwords移动网络
“AdsBot-Google-Mobile (+http://www.google.com/mobile/adsbot.html) Mozilla (iPhone; U; CPU iPhone OS 3 0 like Mac OS X) AppleWebKit (KHTML, like Gecko) Mobile Safari”
微软 Spider UA 必应
“Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)”
腾讯搜搜Spider UA 搜搜
“Sosospider+(+http://help.soso.com/webspider.htm)”
搜搜图片
“Sosoimagespider+(+http://help.soso.com/soso-image-spider.htm)”
雅虎 Spider UA
“Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)”
“Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)”
Sogou Spider UA
搜狗
“http://pic.sogou.com” “Sogou Pic Spider/3.0(+http://www.sogou.com/docs/help/webmasters.htm#07)”
“Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)”
网易有道 UA
“Mozilla/5.0 (compatible; YoudaoBot/1.0; http://www.youdao.com/help/webmaster/spider/; )”
宜搜 EasouSpider
Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)
华为赛门铁克蜘蛛
“HuaweiSymantecSpider/1.0+DSE-support@huaweisymantec.com+(compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR ;
http://www.huaweisymantec.com/cn/IRL/spider)”
七牛镜像蜘蛛
qiniu-imgstg-spider-1.0
监控宝
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; JianKongBao Monitor 1.1)”
DNSPod监控
DNSPod-Monitor/2.0
瑞典 Speedy Spider
“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider/)”
俄罗斯 yandex
“Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)”
俄罗斯 LinkpadBot
Mozilla/5.0 (compatible; LinkpadBot/1.06; +http://www.linkpad.ru)
英国 MJ12bot
“Mozilla/5.0 (compatible; MJ12bot/v1.4.0; http://www
除非注明,网络人的文章均为原创,转载请以链接形式标明本文地址:https://www.55mx.com/post/231
《discuz里用PHP实现的一个检测访问的是否为搜索引擎蛛蛛的封装》的网友评论(0)