Access restrictions on IP and IP segments

Source: Internet
Author: User
Tags foreach explode preg reference
Originally wrote a few functions, IP blacklist is recorded in the library, in order to facilitate understanding, changed to a class of IP blacklist written out.

192.168.1.1 Single IP
192.168.1.* acting like this 192.168.1.1-192.168.1-255
192.158.1.2-20 This is Representative 192.158.1.2-192.158.1.20
You can also write 192.168.1 like this. [1|2|3]

Hey ~ a method does not know whether the idea is comprehensive, take out the discussion

Use

The following is a reference fragment:
$oBlock _ip = new Block_ip ();
$oBlock _ip->checkip ();

The following is a reference fragment:
<?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);
Remove the IP section from the Blacklist
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") && 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);
}

}
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.