PHP restricts IP access to allow only specified IP access allow * Number wildcard filter ip_php tutorial

Source: Internet
Author: User
[PHP]

01./**
02. * Detect whether the IP that is accessed is a specified allowed IP
* Enter description here ...
04. */
05.function Check_ip () {
$ALLOWED _ip=array (' 192.168.2.* ', ' 127.0.0.1 ', ' 192.168.2.49 ');
. $IP =getip ();
$check _ip_arr= explode ('. ', $IP);//IP to be detected split into an array
#限制IP.
if (!in_array ($IP, $ALLOWED _ip)) {
One. foreach ($ALLOWED _ip as $val) {
if (Strpos ($val, ' * ')!==false) {//found with * number substitution
$arr =array ();//
$arr =explode ('. ', $val);
The $BL =true;//is used to record whether there is a successful match in the loop detection.
for ($i =0; $i <4; $i + +) {
if ($arr [$i]!= ' * ') {//is not equal to * will come in the detection, if the * symbol substitute is not checked
if ($arr [$i]!= $check _ip_arr[$i]) {
$BL =false;
break;//stop checking this IP to continue checking the next IP
21.}
22.}
}//end for
if ($BL) {///If True, find a match to be successful returns
return;
. Die;
27.}
28.}
}//end. foreach
Header (' http/1.1 403 Forbidden ');
echo "Access forbidden";
Die;
33.}
34.}
/**
* Detects whether the IP that is accessed is a specified allowed IP
* Enter description here ...
*/
function Check_ip () {
$ALLOWED _ip=array (' 192.168.2.* ', ' 127.0.0.1 ', ' 192.168.2.49 ');
$IP =getip ();
$check _ip_arr= explode ('. ', $IP);//IP to be detected split into an array
#限制IP
if (!in_array ($IP, $ALLOWED _ip)) {
foreach ($ALLOWED _ip as $val) {
if (Strpos ($val, ' * ')!==false) {//found with * number substitution
$arr =array ();//
$arr =explode ('. ', $val);
$BL =true;//is used to record whether there is a successful match in the loop detection
for ($i =0; $i <4; $i + +) {
if ($arr [$i]!= ' * ') {//Not equal to * will come in for detection, if the * symbol substitute is not checked
if ($arr [$i]!= $check _ip_arr[$i]) {
$BL =false;
break;//Terminate check this IP continue check next IP
}
}
}//end for
if ($BL) {///If True, find a match to be successful returns
Return
Die
}
}
}//end foreach
Header (' http/1.1 403 Forbidden ');
echo "Access forbidden";
Die
}
}[php]
01.
[PHP]
01.

/**
02. * Get access to IP
* Enter description here ...


06.
07.
08.
09.}


10.

11.

12. Add a call to Check_ip () where it needs to be detected; To This function provides only the specified IP access to the file and provides the IP in the * number wildcard matching multiple IP www.2cto.com
[PHP]
/** * Get access to IP * Enter description here ... */function GetIP () {return isset ($_server["http_x_forwarded_for"])? $_server[" Http_x_forwarded_for "]:(isset ($_server[" http_client_ip "])? $_server[" Http_client_ip "]: $_server[" REMOTE_ADDR "]); } /**
* Get access to IP
* Enter description here ...
*/
function GetIP () {
return Isset ($_server["Http_x_forwarded_for")? $_server["Http_x_forwarded_for"]
:(Isset ($_server["Http_client_ip")? $_server["Http_client_ip"]
: $_server["REMOTE_ADDR"]);
}


Add a call to Check_ip () where it needs to be detected; To This function provides only the specified IP access to the file and provides the IP in the * number wildcard match multiple IP

http://www.bkjia.com/PHPjc/477824.html www.bkjia.com true http://www.bkjia.com/PHPjc/477824.html techarticle [PHP] 01./** 02. * Detects if the IP access is the specified allowed IP. * Enter description here ... */05.function Check_ip () {$ALLOWED _ip=array (192.168.2.*,127.0.0.1,192.16 ...

  • 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.