This article mainly introduces the PHP Judgment IP blacklist program code example, the need for friends can refer to the following
The school's news system requires some news to only open the campus IP browsing, and then rewrite a code to implement this function, the result is that, as long as the allowed IP access to ip.txt This file can be included, while supporting Class C IP, for example:
Ip.txt
192.168
211.67.188
211.67.191.25
The code is as follows:
The code is as follows:
/** IP Address blacklist, whitelist * Determine if the IP of the guest address 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
The code is as follows:
<?php class Block_ip {var $Block _ip = Array ("192.168.1.1", "210.10.2.1-20", "222.34.4.*"); function construct () {} func tion 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 & Lt;= $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 = $thi S->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); Remove the Blacklist Ip segment if ($iptable) {foreach ($iptable as $value) {if (Preg_match ("{$value}", $Ip)) {$IsJoined = false; break;} }}//If the IP blacklist performs the following actions 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); }}?>
Reference fragment:
The code is as follows:
$oBlock _ip = new Block_ip (); $oBlock _ip->checkip ();