Home  >  Article  >  Backend Development  >  How to use PHP to make the page only accessible to Baidu gogole spider_PHP tutorial

How to use PHP to make the page only accessible to Baidu gogole spider_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:42:09804browse

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);

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn