PHP disables access to the website from some IP addresses _ PHP Tutorial

Source: Internet
Author: User
PHP prohibits individual IP addresses from accessing the website. If you want to prevent an IP address from accessing the website, you can block the IP address. this method is provided below. See the following code. The code for copying the code is as follows: functionget_ip_data () {$ ipfile_get_contents (ht can block an IP address from accessing the website. this method is provided below. See the following code.

The code is as follows:


Function get_ip_data (){
$ Ip = file_get_contents ("http://ip.taobao.com/service/getIpInfo.php? Ip = ". get_client_ip ());
$ Ip = json_decode ($ ip );
If ($ ip-> code ){
Return false;
}
$ Data = (array) $ ip-> data;
If ($ data ['region'] = 'hubei province '&&! IsCrawler ()){
Exit ('http: // www.a.net ');
}
}

Function isCrawler (){
$ SpiderSite = array (
"TencentTraveler ",
"Baiduspider + ",
"BaiduGame ",
"Googlebot ",
"Msnbot ",
"Sosospider + ",
"Sogou web spider ",
"Ia_archiver ",
"Yahoo! Slurp ",
"YoudaoBot ",
"Yahoo Slurp ",
"MSNBot ",
"Java (Often spam bot )",
"Baidusp ",
"Voila ",
"Yandex bot ",
"BSpider ",
"Twiceler ",
"Sogou Spider ",
"Speedy Spider ",
"Google AdSense ",
"Heritrix ",
"Python-urllib ",
"Alexa (IA Archiver )",
"Ask ",
"Exabot ",
"Custo ",
"OutfoxBot/YodaoBot ",
"Yacy ",
"SurveyBot ",
"Legs ",
"Lwp-trivial ",
"Nutch ",
"StackRambler ",
"The web archive (IA Archiver )",
"Perl tool ",
"MJ12bot ",
"Netcraft ",
"MSIECrawler ",
"WGet tools ",
"Larbin ",
"Fish search ",
);
If (in_array (strtolower ($ _ SERVER ['http _ USER_AGENT ']), $ spiderSite )){
Return true;
} Else {
Return false;
}
}

// Obtain the client ip address
Function get_client_ip ()
{
If (isset ($ _ SERVER )){
If (isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {
$ Realip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
} Else if (isset ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Realip = $ _ SERVER ["HTTP_CLIENT_IP"];
} Else {
$ Realip = $ _ SERVER ["REMOTE_ADDR"];
}
} Else {
If (getenv ("HTTP_X_FORWARDED_FOR ")){
$ Realip = getenv ("HTTP_X_FORWARDED_FOR ");
} Else if (getenv ("HTTP_CLIENT_IP ")){
$ Realip = getenv ("HTTP_CLIENT_IP ");
} Else {
$ Realip = getenv ("REMOTE_ADDR ");
}
}
Return $ realip;
}

Bytes. See the following code. The code is as follows: function get_ip_data () {$ ip = file_get_contents ("ht...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.