IP及IP段进行访问限制

IP及IP段进行访问限制
转自:[url]http://hudeyong926.iteye.com/blog/1584554[/url]


<?php
$oblock_ip = new block_ip();
$boolean = $oblock_ip->checkip();
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['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], "unknown"))
$ip = $_server['remote_addr'];
else
$ip = "unknown";
return($ip);
}

}
?>



/


<?php
/*
子网掩码“长”得和 IP 地址类似,比如:255.255.255.0。可以用它来做逻辑 AND 运算判断是否要把数据
包发送到外网(区分子网) 。比如我们已知 IP 为 202.202.15.0,子网掩码为 255.255.255.0(24 个 1) ,那么
我们可以把这个网段描述为202.202.15.0:24。 现在我要判断202.212.16.238这个地址是否在202.202.15.0:24
这个网络中。可以这么做:
由于掩码为 24 位,你可以理解为只要IP 地址和网络地址的前 24 位相同,那就是同一个网络了。
先用二进制来实现。把24(即 255.255.255.0)转换成掩码,也就是 24 个 1。
11111111.11111111.11111111.00000000
然后,11111111 11111111 11111111 00000000(255.255.255.0)
AND 11001010 11001010 00010000 11101110(202.212.16.238)
=11001010 11001010 00010000 00000000(202.202.16.0)
202.202.16.0!=202.202.15.0,所以不在网络地址内。
用 PHP 代码来表示如下:

*/
function ip_in_network($ip, $net_addr, $net_mask){
if($net_mask <= 0){ return false; }
$ip_binary_string = sprintf("%032b",ip2long($ip));
$net_binary_string = sprintf("%032b",ip2long($net_addr));
return (substr_compare($ip_binary_string,$net_binary_string,0,$net_mask) === 0);
}

var_dump(ip_in_network('202.202.16.0', '202.202.16.10', 24));

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值