Many times, webmasters are very concerned about whether their website's SEO is up to standard. Therefore, when checking various webmaster tools, we also want to know which crawlers have visited our website, because this is related to how search engines favor our website.
Therefore, we can monitor spider visits by checking the website logs. Today, I will share a method for WordPress to use PHP code to record spider visit logs. Here is the code.
// 统计蜘蛛
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);
}
Note
Before adding the code, you need to create a txt text file named robotslogs.txt in the website's root directory and set its permissions to 777. Then, access the file to view the spider visit records. It's very convenient.