Using php to make pages accessible only by Baidu gogole Spider
Source: Internet
Author: User
Using php to make the page accessible only by Baidu gogole spider, you can refer to the following if you need it. The difference between a common user and a search engine spider crawling is that the user agent sent,
Looking at the website log file, we can find that Baidu spider's name contains Baiduspider, while google's name is Googlebot. in this way, we can determine whether to cancel normal user access by judging the user agent sent, write functions as follows:
The code is as follows:
Function isAllowAccess ($ directForbidden = FALSE ){
$ Allowed = array ('/baidusp/ 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;
}
It is okay to reference this function in the header of the page to be blocked for judgment. the call method is as follows:
The code is as follows:
If (! IsAllowAccess ()){
Exit ("404 not found ");
}
// Or
IsAllowAccess (TRUE );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.