将以下代码放入主题目录下 functions.php 文件。
//记录蜘蛛来访 function get_naps_bot(){ $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($useragent, 'Googlebot') !== false){ return 'Googlebot'; } if (strpos($useragent, 'msnbot') !== false){ return 'MSNbot'; } if (strpos($useragent, 'slurp') !== false){ return 'Yahoobot'; } if (strpos($useragent, 'Baiduspider') !== false){ return 'Baiduspider'; } if (strpos($useragent, 'sohu-search') !== false){ return 'Sohubot'; } if (strpos($useragent, '360Spider') !== false){ return '360Spider'; } if (strpos($useragent, 'Sosospider') !== false){ return 'Sosospider'; } if (strpos($useragent, 'bingbot') !== false){ return 'bingbot'; } if (strpos($useragent, 'Sogouspider') !== false){ return 'Sogouspider'; } return false; } function nowtime(){ date_default_timezone_set('Asia/Shanghai'); $date=date("Y-m-d.G:i:s"); return $date; } $searchbot = get_naps_bot(); if ($searchbot) { $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); $url=$_SERVER['HTTP_REFERER']; $file="robotslogs.txt"; $time=nowtime(); $data=fopen($file,"a"); $PR="$_SERVER[REQUEST_URI]"; fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n page:$PR\r\n"); fclose($data); }
然后需要在根目录新建一个 txt 文本 robotslogs.txt,权限设置为 777,以后访问 http://your_domain/robotslogs.txt 便可以很仔细的看到蜘蛛的来访记载了。
如果你用的主题经常更新,functions 文件可能需要经常修改,那么可以将这段代码做成插件,这样主题更新就不会覆盖代码了
插件代码如下
<?php /* Plugin Name: 蜘蛛统计 Description: 统计蜘蛛来访日志 Author: */ //统计蜘蛛 function get_naps_bot(){ $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($useragent, 'Googlebot') !== false){ return 'Googlebot'; } if (strpos($useragent, 'msnbot') !== false){ return 'MSNbot'; } if (strpos($useragent, 'slurp') !== false){ return 'Yahoobot'; } if (strpos($useragent, 'Baiduspider') !== false){ return 'Baiduspider'; } if (strpos($useragent, 'sohu-search') !== false){ return 'Sohubot'; } if (strpos($useragent, '360Spider') !== false){ return '360Spider'; } if (strpos($useragent, 'Sosospider') !== false){ return 'Sosospider'; } if (strpos($useragent, 'bingbot') !== false){ return 'bingbot'; } if (strpos($useragent, 'Sogouspider') !== false){ return 'Sogouspider'; } return false; } function nowtime(){ date_default_timezone_set('Asia/Shanghai'); $date=date("Y-m-d.G:i:s"); return $date; } $searchbot = get_naps_bot(); if ($searchbot) { $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); $url=$_SERVER['HTTP_REFERER']; $file="robotslogs.txt"; $time=nowtime(); $data=fopen($file,"a"); $PR="$_SERVER[REQUEST_URI]"; fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n page:$PR\r\n"); fclose($data); } ?>
把上面的代码自己另存为一个 php 文件,记住是 UTF-8 编码,然后放进 wp-content/plugins 文件夹,在后台插件的菜单里启动插件即可
当然,添加 robotslogs.txt 文件这一步骤也是要做的