多くの場合、サイト管理者は自分のウェブサイトの SEO 対策が十分にできているかどうかを非常に気にしています。そのため、さまざまなウェブマスターツールを確認する際には、どのクローラーが自分たちのウェブサイトにアクセスしたのかも知りたいと思うでしょう。これは、検索エンジンが私たちのウェブサイトをどの程度評価しているかに関係するためです。
そこで、ウェブサイトのログを確認することで、クローラーの訪問状況を監視できます。今回は、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, '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 に設定する必要があります。その後、このファイルにアクセスすれば、クローラーの訪問記録を確認できます。とても便利ですよ。