The difference between ordinary users and search engine spider crawlers is the user agent sent.
Looking at the website log file, you can find that the Baidu spider name contains Baiduspider, while Google’s is Googlebot. In this way, we can decide whether to use it by judging the user agent sent. To cancel the access of ordinary users, write the function as follows:
Copy the code The code is as follows:
function isAllowAccess($directForbidden = FALSE) {
$allowed = array('/baiduspider/i', '/googlebot/i');
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$valid = FALSE;
foreach ($allowed as $pattern) {
if (preg_match($pattern, $user_agent)) {
$valid = TRUE;
break;
}
}
if (! $valid && $directForbidden) {
exit("404 not found");
}
return $valid;
}
To prohibit access It is ok to refer to this function in the header of the page for judgment. The calling methods are as follows:
Copy the code The code is as follows:
if (!isAllowAccess()) {
exit("404 not found");
}
//or
isAllowAccess(TRUE);
http://www.bkjia.com/PHPjc/321062.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321062.htmlTechArticleThe difference between ordinary users and search engine spider crawlers is the user agent sent. You can find the Baidu spider name by looking at the website log file It includes Baiduspider, and Google’s is Googlebot, so we...