This article mainly introduces PHP to determine the IP blacklist program code example, the need for friends can refer to the following
The school's news system requires some news only to open the campus IP browsing, so rewrite a code to implement this function, the result is, as long as the IP access allowed to be included in Ip.txt this file can, while supporting the C-class IP, such as: Ip.txt 192.168 211.67.188 211.67.191.25 code is as follows: code is as follows: * * IP address blacklist, whitelist * to determine whether the guest address IP is in Ip.txt, support Class C IP * by xhat/ $IP = $_server[' REMOTE_ADDR ']; $ipArray = Preg_replace ("#rn? | n# "," ", File (' Ip.txt ')); foreach ($ipArray as $ipTest) {if (Substr_count ($ip, $ipTest)!= "0") {echo "OK";//Execute Related command die ();} ?>   ; Above you can use code to cheat, the following code to upgrade code as follows: <?php 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) { 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; //get user ip $Ip = $this->get_client_ip (); $Ip = Trim ($Ip); //blacklist Ip sector if ($iptable) { foreach ($iptable as $value) { if (preg_match ("{$value}", $Ip)) { $IsJoined = false; break; } } } //If you do the following in the IP blacklist if (! $IsJoined) { echo "IP Error"; exit; } } Private Function get_client_ip () { if (getenv ("Http_client_ip") & amp;& 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 '] & amp;& strcasecmp ($_server[' remote_addr '], "unknown")) $ip = $_server[' remote_addr ']; else $ip = ' Unknown "; return ($IP); } } ?> Reference fragment: code as follows: $oBlock _ip = new Block_ip ( ); $oBlock _ip->checkip ();