WordPress 統計搜尋引擎蜘蛛程式碼
4,364 0
很多時候,站長們都很關心自己的網站 SEO 做得是否到位,因此我們在查看各種站長工具時,也會想知道哪些爬蟲造訪過我們的網站,因為這關係到搜尋引擎對我們網站的偏好。
因此,我們可以透過查看網站日誌來監控蜘蛛的造訪情況。今天我要分享的是一段 WordPress 利用 PHP 程式碼記錄蜘蛛造訪日誌的方式,下面是程式碼。
PHP 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, 'lycos') !== false) {
return 'Lycos';
}
if (strpos($useragent, 'robozilla') !== false) {
return 'Robozilla';
}
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。然後造訪該檔案,就可以看到蜘蛛的造訪記錄了,非常方便哦。
評論
(0)暫無評論,來說兩句吧