PHP IP and IP segment access restriction code_PHP tutorial

WBOY
Release: 2016-07-21 15:48:46
Original
1008 people have browsed it

192.168.1.1 A single IP
192.168.1.* Agent 192.168.1.1-192.168.1-255
192.158.1.2-20 This is a representative 192.158.1.2-192.158.1.20
.1.[1|2|3]
Hey~ I don’t know if the idea is comprehensive or not, let’s bring it up for discussion
Use
The following is a quote:
$oBlock_ip = new block_ip();
$oBlock_ip->checkIP();
The following is a reference fragment:

Copy code The code is as follows:

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 it 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);
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319682.htmlTechArticle192.168.1.1 Single IP 192.168.1.* This proxy 192.168.1.1-192.168.1-255 192.158.1.2 -20 This represents 192.158.1.2-192.158.1.20. You can also write 192.168.1.[1|2|3] Hey ~ a way...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!