php ip2long mysql_PHP: ip2long - Manual

Hi,

based on examples above I have mixed class IPFilter with netMatch function wich gives me complete class for IP4 check including CIDR IP format:

private static$_IP_TYPE_SINGLE='single';

private static$_IP_TYPE_WILDCARD='wildcard';

private static$_IP_TYPE_MASK='mask';

private static$_IP_TYPE_CIDR='CIDR';

private static$_IP_TYPE_SECTION='section';

private$_allowed_ips= array();

public function__construct($allowed_ips) {$this->_allowed_ips=$allowed_ips;

}

public functioncheck($ip,$allowed_ips=null) {$allowed_ips=$allowed_ips?$allowed_ips:$this->_allowed_ips;

foreach ($allowed_ipsas$allowed_ip) {$type=$this->_judge_ip_type($allowed_ip);$sub_rst=call_user_func(array($this,'_sub_checker_'.$type),$allowed_ip,$ip);

if ($sub_rst) {

returntrue;

}

}

returnfalse;

}

private function_judge_ip_type($ip) {

if (strpos($ip,'*')) {

returnself::$_IP_TYPE_WILDCARD;

}

if (strpos($ip,'/')) {$tmp=explode('/',$ip);

if (strpos($tmp[1],'.')) {

returnself::$_IP_TYPE_MASK;

} else {

returnself::$_IP_TYPE_CIDR;

}

}

if (strpos($ip,'-')) {

returnself::$_IP_TYPE_SECTION;

}

if (ip2long($ip)) {

returnself::$_IP_TYPE_SINGLE;

}

returnfalse;

}

private function_sub_checker_single($allowed_ip,$ip) {

return (ip2long($allowed_ip) ==ip2long($ip));

}

private function_sub_checker_wildcard($allowed_ip,$ip) {$allowed_ip_arr=explode('.',$allowed_ip);$ip_arr=explode('.',$ip);

for ($i=0;$i

if ($allowed_ip_arr[$i] =='*') {

returntrue;

} else {

if (false== ($allowed_ip_arr[$i] ==$ip_arr[$i])) {

returnfalse;

}

}

}

}

private function_sub_checker_mask($allowed_ip,$ip) {

list($allowed_ip_ip,$allowed_ip_mask) =explode('/',$allowed_ip);$begin= (ip2long($allowed_ip_ip) &ip2long($allowed_ip_mask)) +1;$end= (ip2long($allowed_ip_ip) | (~ip2long($allowed_ip_mask))) +1;$ip=ip2long($ip);

return ($ip>=$begin&&$ip<=$end);

}

private function_sub_checker_section($allowed_ip,$ip) {

list($begin,$end) =explode('-',$allowed_ip);$begin=ip2long($begin);$end=ip2long($end);$ip=ip2long($ip);

return ($ip>=$begin&&$ip<=$end);

}

private function_sub_checker_CIDR($CIDR,$IP) {

list ($net,$mask) =explode('/',$CIDR);

return (ip2long($IP) & ~((1<< (32-$mask)) -1) ) ==ip2long($net);

}

}?>

For me this code works great, so I wanna thank to all You guys!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值