2012MTR

WordPress 如何判断访客是否爬虫蜘蛛?

WordPress「PHP」如何判断访客是否爬虫蜘蛛?搜索引擎蜘蛛都有自己特有标识,可以通过 HTTP_USER_AGENT 来判断是否是蜘蛛,代码提取、修改自 WP-PostViews 插件。

isCrawler 代码如下:

if ( !function_exists( 'isCrawler' ) ) {
//https://xuv.cc/ 提取自 WP-PostViews 插件 https://wordpress.org/plugins/wp-postviews
function isCrawler() {
$bots = array(
'Google Bot' => 'google'
, 'MSN' => 'msnbot'
, 'Alex' => 'ia_archiver'
, 'Lycos' => 'lycos'
, 'Ask Jeeves' => 'jeeves'
, 'Altavista' => 'scooter'
, 'AllTheWeb' => 'fast-webcrawler'
, 'Inktomi' => 'slurp@inktomi'
, 'Turnitin.com' => 'turnitinbot'
, 'Technorati' => 'technorati'
, 'Yahoo' => 'yahoo'
, 'Findexa' => 'findexa'
, 'NextLinks' => 'findlinks'
, 'Gais' => 'gaisbo'
, 'WiseNut' => 'zyborg'
, 'WhoisSource' => 'surveybot'
, 'Bloglines' => 'bloglines'
, 'BlogSearch' => 'blogsearch'
, 'PubSub' => 'pubsub'
, 'Syndic8' => 'syndic8'
, 'RadioUserland' => 'userland'
, 'Gigabot' => 'gigabot'
, 'Become.com' => 'become.com'
, 'Baidu' => 'baiduspider'
, 'so.com' => '360spider'
, 'Sogou' => 'spider'
, 'soso.com' => 'sosospider'
, 'Yandex' => 'yandex'
);
$useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
foreach ( $bots as $name => $lookfor ) {
if ( ! empty( $useragent ) && ( false !== stripos( $useragent, $lookfor ) ) ) {
return true;
}
}
return false;
}
}

折腾 WordPress 主题可能用得上,直接使用 isCrawler() 判断,爬虫返回 true,否则 false。

退出移动版