对于wordpress来说,记录搜索引擎爬行记录的插件应该有很多。但是我们也知道,插件过多势必会影响页面加载速度,所以我给大家提供一段代码,来记录搜索引擎的爬行信息,这段代码很早以前就有人在网上放出来了。
监视搜索引擎蜘蛛爬行记录的代码:
<?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, 'sosospider') !== false){
return 'sosospider';
}
if (strpos($useragent, 'sogou spider') !== false){
return 'sogou spider';
}
if (strpos($useragent, 'YoudaoBot') !== false){
return 'YoudaoBot';
}
return false;
}
function nowtime(){
$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");
fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n");
fclose($data);
}
?>
这段代码主要利用各大搜索引擎蜘蛛爬行程序都有自己的标识符,google的标识符为:googlebot,百度的标识符为:baiduspider,它们可以通过查询_SERVER变量中的HTTP_USER_AGENT参数来获取,再结合HTTP_REFERER记录下的URL的来源,最后用nowtime()函数来获取当前系统时间,最后将是那个面得到的参数写入robotslogs.txt文档中。
新建一个php文件(名字自定),例如spider.php。最后上传到wordpress当前正在使用主题的目录中,最后在主题的foot.php中调用spider.php文件。
<?php include('spider.php') ?>
通过一段时间的运行,你就可以通过http://www.youdomain.com/robotslogs.txt来查看搜索引擎蜘蛛的访问记录了。