Home  >  Article  >  Backend Development  >  Code sharing for restricting IP segment access and prohibiting IP submission form in PHP, IP form_PHP tutorial

Code sharing for restricting IP segment access and prohibiting IP submission form in PHP, IP form_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:20:09777browse

Restrict ip segment access and prohibit ip submission form code sharing in php, ip form

Just add the following code to the page where access or form submission needs to be prohibited for judgment.

Note: The following is just an example code of PHP restricting IP. If you plan to apply it to CMS, please modify it yourself.

<&#63;php 
/加IP访问限制 
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') && 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']; 
} 
//限制ip 
if ($userip=='192.168.1.88'){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
} 
//限制ip段 
$ip_arr = explode('.', $userip); 
#限制的ip段,假设是192.168.*.* 
if (!(($ip_arr[0] == '192' && $ip_arr[1]=='168') )){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
}else{ 
header("location:http://afish.cnblogs.com");//正常IP则直接访问小鱼阁首页 
exit; 
} 
&#63;> 

PHP code that restricts access to IP address segments

6f958ba5b22c8d4c29a642be2c55e5d4=195)&&($userips[1]e9c15cf3e376691e9938af79c6f3dc58 =163)&&($userips[2]6bd2ef2a8483e10de02a98bbc6abeac5=072)&&($userips[3]58763083f05a8128c382015d870632c1=000)){
echo "Your IP does not match!";
exit;
}else{
echo "Verified!";
}
?>

Mine The idea is like this
Get the user's IP and use "." to split it into an array
and then use "." to split the restricted IP blocks into arrays

and then determine whether the user's IP is within this IP block. (Judge whether the four elements are within this range respectively)
...
The following is one found in Blue Ideal for your reference
———————————————— ————
class IP{ //Get 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='127.0.0.1';
}
}
function get_netip($ myip){ //Only keep the first three digits of the customer’s IP address
$ temp=explode(" .",$ myip);
$ netip.=$ temp[0];
$ netip.=".";
$ netip.=$ temp[1];
$ netip. =".";
$ netip.=$ temp[2];
return $ netip;
}
$ filename="test.ini"; //Define operation file
$ ip_lib=file($ filename); //Read file data into the array
$ allow=0;
$ IP=new IP;
$ thisip="";
$ IP-> ;getIpAdr(&$ thisip);
$ thenetip=get_netip($ thisip);

for($ i=0;$ iif (ereg($ thenetip,$ ip_lib[$ i])){
$ allow=1;
break;
}
}

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

Help Man look at a string of PHP code, how to realize the forbidden IP to display web content

if (in_array($ip, $blacklist)){ //Block IP access in the blacklist
header('HTTP/1.0 403 Forbidden');
echo "Forbidden access";
exit;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868247.htmlTechArticleRestrict ip segment access and prohibit ip submission form code sharing in php. The ip form needs to prohibit access or form submission. Just add the following code to the page to make a judgment. Note: Below...
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