Php restricts ip segment access and prohibits code sharing of ip submission forms. ip form

Source: Internet
Author: User

Php restricts ip segment access and prohibits code sharing of ip submission forms. ip form

Add the following code on the page where you want to disable access or submit a form for determination.

Note: The following is only an example code of php ip address restriction. If you want to apply it to CMS, modify it on your own.

<? Php/Add IP address access restriction if (getenv ('HTTP _ CLIENT_IP ') & strcasecmp (getenv ('HTTP _ CLIENT_IP'), 'unknown ')) {$ userip = getenv ('HTTP _ CLIENT_IP ');} elseif (getenv ('HTTP _ X_FORWARDED_FOR') & strcasecmp (getenv ('HTTP _ X_FORWARDED_FOR '), 'unknon') {$ userip = getenv ('HTTP _ X_FORWARDED_FOR ');} elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR '), 'unknown') {$ userip = getenv ('remote _ ADDR ');} elseif (isse T ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR'] & strcasecmp ($ _ SERVER ['remote _ ADDR '], 'unknon') {$ userip = $ _ SERVER ['remote _ ADDR '];} // restrict ip address if ($ userip = '2017. 168.1.88 ') {header ("location: http://t.qq.com/wb631992791"); // jump to Weibo exit after being disabled;} // restrict ip segment $ ip_arr = explode ('. ', $ userip); # The restricted ip segment, which is assumed to be 192. 168. *. * if (! ($ Ip_arr [0] = '000000' & $ ip_arr [1] = '000000') {header ("location: http://t.qq.com/wb631992791 "); // jump to the microblog exit after being banned;} else {header ("location: http://afish.cnblogs.com"); // normal IP Access to the home page of ainemo pavilion exit;}?>


Php Code restricting IP address segment access

<? Php
$ Userip = $ _ SERVER ['remote _ ADDR ']; // obtain the user ip Address
$ Userips = explode (".", $ userip); // cut the obtained ip address into an array.

If ($ userips [0] <= 195 & $ userips [0]> = 195) & ($ userips [1] <=163 & $ userips [1]> = 163) & ($ userips [2] <= 079 & $ userips [2]> = 072) & ($ userips [3] <= 255 & $ userips [3]> = 000 )){
Echo "your ip address does not match! ";
Exit;
} Else {
Echo "verified! ";
}
?>

This is my idea.
Use "." To split the user's IP address into arrays.
Then, separate the restricted IP address with "." into an array.

Then determine whether the user's IP address is in this IP address segment (determine whether the four elements are in this range)
......
Here is a blue ideal for your reference.
------------------
<? Php
Class IP {// obtain the customer IP Address
Function getIpAdr (& $ ip ){
$ Ip1 = getenv ("HTTP_X_FORWARDED_FOR ");
$ Ip2 = getenv ("HTTP_CLIENT_IP ");
$ Ip3 = getenv ("REMOTE_ADDR ");
If ($ ip1 & $ ip1! = 'Unknow ')
$ Ip = $ ip1;
Else if ($ ip2 & $ ip2! = 'Unknow ')
$ Ip = $ ip2;
Else if ($ ip3 & $ ip3! = 'Unknow ')
$ Ip = $ ip3;
Else
$ Ip = '1970. 0.0.1 ';
}
}
Function get_netip ($ myip) {// only the first three IP addresses of the customer are left
$ Temp = explode (".", $ myip );
$ Netip. = $ temp [0];
$ Netip. = ".";
$ Netip. = $ temp [1];
$ Netip. = ".";
$ Netip. = $ temp [2];
Return $ netip;
}
$ Filename = "test. ini"; // defines the operation File
$ Ip_lib = file ($ filename); // read file data to the array
$ Allow = 0;
$ IP = new IP;
$ Thisip = "";
$ IP-> getIpAdr (& $ thisip );
$ Thenetip = get_netip ($ thisip );

For ($ I = 0; $ I <count ($ ip_lib); $ I ++ ){
If (ereg ($ thenetip, $ ip_lib [$ I]) {
$ Allow = 1;
Break;
}
}

If ($ allow = 1 ...... remaining full text>

How can I display webpages with banned IP addresses when I look at a bunch of PHP code?

If (in_array ($ ip, $ blacklist) {// block IP access from the blacklist
Header ('HTTP/1.0 403 Forbidden ');
Echo "Access prohibited ";
Exit;
}

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.