The IP blacklist implementation method is to first put the IP to be filtered in a txt file or database, and then obtain our IP address when we visit and then traverse and compare it.
The school's news system requires that some news can only be browsed by IP addresses within the school, so a code was rewritten to implement this function. The result after implementation is that as long as the IP addresses allowed to be accessed are listed in the ip.txt file, it also supports c Class IP, for example:
ip.txt
192.168
211.67.188
211.67.191.25
The code is as follows:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
/*
* 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();
}
}
?>
|
/*
* IP address blacklist, whitelist
* Determine whether the ip of the visitor's address is in ip.txt, and supports class c ip
代码如下 |
复制代码 |
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;
}
}
}
// www.bKjia.c0m如果在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['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($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 commands
die();
}
}
?>
|
You can use the above code to cheat, the following code is to upgrade
The code is as follows |
Copy code |
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);
//Remove IP segments from the blacklist
if ($iptable) {
foreach($iptable as $value) {
if (preg_match("{$value}",$Ip)) {
$IsJoined = false;
break;
}
}
}
// If www.bKjia.c0m is in the IP blacklist, perform the following operations
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);
}
}
?>
|
Quote:
The code is as follows
代码如下 |
复制代码 |
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();
|
|
Copy code |
|
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();
http://www.bkjia.com/PHPjc/632711.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632711.htmlThe implementation method of ip blacklist is to first put the IP to be filtered in a txt file or database, and then when we This can be achieved by obtaining our IP address when visiting and then traversing and comparing it. School...
Statement: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