PHP restricted IP segment access, prohibit IP submission form code sharing, IP form _php Tutorial

Source: Internet
Author: User

Restrict IP segment access in PHP, disable code Sharing for IP submission forms, IP forms


Add the following code to the page where you want to prohibit access or submit a form.

Note: Below is just an instance code that restricts IP to PHP, and if you intend to apply it to a CMS, modify it yourself.

<?php/plus IP access limit 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 '), ' unknown ') {$userip = getenv (' http_x_forwarded_for ');} elseif (getenv (' remote_addr ') && Amp STRCASECMP (getenv (' remote_addr '), ' unknown ') {$userip = getenv (' remote_addr ');} elseif (Isset ($_server[' Remote_ ADDR ']) && $_server[' remote_addr '] && strcasecmp ($_server[' remote_addr '), ' unknown ') {$userip = $_ server[' REMOTE_ADDR ']; }//Restrict IP if ($userip = = ' 192.168.1.88 ') {header ("location:http://t.qq.com/wb631992791");//is disabled and then jumps to the Weibo exit;}//Limit IP segment $ip _ arr = explode ('. ', $userip); #限制的ip段, suppose it is 192.168.*.* if (!) ( ($ip _arr[0] = = ' 192 ' && $ip _arr[1]== ' 168 '))) {Header ("location:http://t.qq.com/wb631992791");//is forbidden to jump to Weibo exit;} else{header ("location:http://afish.cnblogs.com");//normal IP direct access to the small fish Court home exit;}?> 


PHP code that restricts access to IP address segments

$userip = $_server[' remote_addr '); Get the User IP
$userips = Explode (".", $userip);//Cut the acquired IP 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 does not match!";
Exit
}else{
echo "by verification!";
}
?>

That's what I thought.
Get the user's IP with "." Split array
Then use "." To separate the restricted IP. Split array

Then determine whether the user IP is within this IP fault (determine whether four elements are within this range)
......
Here is a blue ideal for your reference
——————————————————
Class ip{//Get 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= ' 127.0.0.1 ';
}
}
function Get_netip ($ myip) {//Leave only the first three bits of the client IP address
$ temp=explode (".", $ myip);
$ netip.=$ temp[0];
$ netip.= ".";
$ netip.=$ temp[1];
$ netip.= ".";
$ netip.=$ temp[2];
return $ netip;
}
$ filename= "Test.ini"; Defining Action Files
$ ip_lib=file ($ filename); Reads the file data into the array
$ allow=0;
$ ip=new IP;
$ thisip= "";
$ IP-&GT;GETIPADR (&$ Thisip);
$ thenetip=get_netip ($ thisip);

For ($ i=0;$ I
if (Ereg ($ thenetip,$ ip_lib[$ i])) {
$ allow=1;
Break
}
}

if ($ allow==1 ...) the rest of the full text >>

Help full look at a bunch of PHP code, how to implement the Forbidden IP Display page content

if (In_array ($ip, $blacklist)) {//block IP Access in blacklist
Header (' http/1.0 403 Forbidden ');
echo "No access";
Exit
}

http://www.bkjia.com/PHPjc/868247.html www.bkjia.com true http://www.bkjia.com/PHPjc/868247.html techarticle PHP restricts access to IP segments, disables the code sharing of IP submission forms, and the IP form can be judged by adding the following code to a page that requires no access or submission of forms. Note: below ...

  • Related Article

    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.