The implementation principle is relatively simple. You only need to obtain the user IP address and then verify whether the current IP address exists in our blacklist library. The specific example is as follows. The implementation principle is relatively simple. You only need to obtain the user IP address and then verify whether the current IP address exists in our blacklist library. The specific example is as follows.
Script ec (2); script
The code segment is found when I search for related solutions on the Internet. The logic of the makePregIP function of this class is a bit problematic. I have modified it and can use it. This function allows access from IP addresses in the whitelist. to restrict access from IP addresses in the blacklist, simply modify the code logic in the checkIP function.
Usage
The Code is as follows: |
|
$ Allow_ip = array ("192.168.1.1", "210.10.2.1-20", "222.34.4. *", "127.0.0.1 "); $ OBlock_ip = new allowIp ($ allow_ip ); If (! $ OBlock_ip-> checkIP ()){ Echo 'your IP address is :'; Echo $ oBlock_ip-> ip; Exit ('Access prohibited '); } |
AllowIP files
The Code is as follows: |
|
Class allowIp { Function _ construct ($ allow_ip ){ If (empty ($ allow_ip )){ Return false; } $ This-> allow_ip = $ allow_ip; $ This-> ip = ''; } Private function makePregIP ($ str) { If (strstr ($ str ,"-")){ $ AIP = explode (".", $ str ); Foreach ($ aIP as $ k =>v v ){ If (! Strstr ($ v ,"-")){ $ Preg_limit. = $ this-> makePregIP ($ v ); $ Preg_limit. = "."; } 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-> allow_ip ){ $ I = 1; Foreach ($ this-> allow_ip as $ k => $ v ){ $ Ipaddres = $ this-> makePregIP ($ v ); $ Ip = str_ireplace (".", ".", $ ipaddres ); $ Ip = str_replace ("*", "[0-9] {1, 3}", $ ip ); $ Ipaddres = "/". $ ip ."/"; $ Ip_list [] = $ ipaddres; $ I ++; } } Return $ ip_list; } Public function checkIP (){ $ Iptable = $ this-> getAllBlockIP (); $ IsJoined = false; // Obtain the user ip Address $ Ip = $ this-> get_client_ip (); $ Ip = trim ($ Ip ); // In the whitelist If ($ iptable ){ Foreach ($ iptable as $ value ){ If (preg_match ("{$ value}", $ Ip )){ $ IsJoined = true; Break; } } } // The IP address is not in the whitelist. If (! $ IsJoined ){ Return false; } Return true; } 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 "; $ This-> ip = $ ip; Return ($ ip ); } } |