Home > Backend Development > PHP Tutorial > Detailed explanation of PHP judgment IP blacklist program code example

Detailed explanation of PHP judgment IP blacklist program code example

怪我咯
Release: 2023-03-13 09:32:01
Original
1761 people have browsed it

This article mainly introduces the code example of PHP judging IP blacklist program. Friends who need it can refer to it

The school’s news system requires that some news can only be browsed by the school IP, so I rewrote a code to To implement this function, the result after implementation is that you only need to list the IPs that are allowed to be accessed in the ip.txt file. It also supports Class C IPs, for example:

ip.txt
192.168
211.67.188
211.67.191.25
The code is as follows:

The code is as follows:

/*
* ip地址黑名单、白名单
* 判断访客地址的ip是否在ip.txt中,支持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"; //执行相关命令
die();
}
}
?>
Copy after login

You can use the above code to cheat, the following code is for 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(){ 
} 
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; 
//取得用户ip 
$Ip = $this->get_client_ip(); 
$Ip = trim($Ip); 
//剔除黑名单中的IP区段 
if ($iptable) { 
foreach($iptable as $value) { 
if (preg_match("{$value}",$Ip)) { 
$IsJoined = false; 
break; 
} 
} 
} 
// 如果在ip黑名单中就执行如下操作 
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[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], "unknown")) 
$ip = $_SERVER[&#39;REMOTE_ADDR&#39;]; 
else 
$ip = "unknown"; 
return($ip); 
} 
} 
?>
Copy after login

Reference fragment:

The code is as follows:

$oBlock_ip = new block_ip(); 
$oBlock_ip->checkIP();
Copy after login


The above is the detailed content of Detailed explanation of PHP judgment IP blacklist program code example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template