Php ip address and IP address segment access restriction code. 192.168.1.1 single IP192.168.1. * proxy 192.168.1.1-192.168.1-255192.158.1.2-20 this represents 192.158.1.2-192.158.1.20. you can also write 192.168.1. [1 | 2 | 3] ~ One method: 192.168.1.1 single IP address
192.168.1. * proxy 192.168.1.1-192.168.1-255
192.158.1.2-20 represents 192.158.1.2-192.158.1.20
You can also write 192.168.1. [1 | 2 | 3]
Hey hey ~ I don't know whether the idea is comprehensive in one method. I will discuss it with you.
Use
The following is a reference clip:
$ OBlock_ip = new block_ip ();
$ OBlock_ip-> checkIP ();
The following is a reference clip:
The code is as follows:
Class block_ip {
Var $ Block_ip = array ("192.168.1.1", "210.10.2.1-20", "222.34.4 .*");
Function _ construct (){
}
Function _ destruct (){
}
Private function makePregIP ($ str ){
If (strstr ($ str ,"-")){
$ AIP = explode (".", $ str );
Foreach ($ aIP as $ k =>v v ){
If (! Strstr ($ v ,"-")){
$ Preg_limit. = makePregIP ($ v );
} Else {
$ AipNum = explode ("-", $ v );
For ($ I = $ aipNum [0]; $ I <= $ aipNum [1]; $ I ++ ){
$ Preg. = $ preg? "|". $ I: "[". $ I;
}
$ Preg_limit. = strrpos ($ preg_limit, ".", 1) = (strlen ($ preg_limit)-1 )? $ Preg. "]": ".". $ preg. "]";
}
}
} Else {
$ Preg_limit. = $ str .".";
}
Return $ preg_limit;
}
Private function getAllBlockIP (){
If ($ this-> Block_ip ){
Foreach ($ this-> Block_ip as $ k => $ v ){
$ Ipaddres = $ this-> makePregIP ($ v-> start_ip );
$ Ip = str_ireplace (".", "\.", $ ipaddres );
$ Ip = str_replace ("*", "[0-9] {1, 3}", $ ip );
$ Ipaddres = "/". $ ip ."/";
$ Ip_list [] = $ ipaddres;
}
}
Return $ ip_list;
}
Public function checkIP (){
$ Iptable = $ this-> getAllBlockIP ();
$ IsJoined = true;
// Obtain the user ip address
$ Ip = $ this-> get_client_ip ();
$ Ip = trim ($ Ip );
// Remove the IP segments in the blacklist
If ($ iptable ){
Foreach ($ iptable as $ value ){
If (preg_match ("{$ value}", $ Ip )){
$ IsJoined = false;
Break;
}
}
}
// Perform the following operations in the ip blacklist:
If (! $ IsJoined ){
Echo "IP Error ";
Exit;
}
}
Private function get_client_ip (){
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown "))
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
$ Ip = getenv ("REMOTE_ADDR ");
Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown "))
$ Ip = $ _ SERVER ['remote _ ADDR '];
Else
$ Ip = "unknown ";
Return ($ ip );
}
}
?>
Http://www.bkjia.com/PHPjc/319682.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319682.htmlTechArticle192.168.1.1 single IP 192.168.1. * This proxy 192.168.1.1-192.168.1-255 192.158.1.2-20 this represents 192.158.1.2-192.158.1.20 can also write 192.168.1. [1 | 2 | 3] ~ A method...