您的位置:首页 > 编程语言 > PHP开发

php 过滤正则ip

2017-01-21 18:25 197 查看

php 过滤正则ip

/**
* 过滤正则ip
* @param $dest_ip 请求ip
* @param $allow_ip ip允许正则 如 10.1.2.* 或 10.1.2.[10,22]或10.1.2.[10-22] 或 [10,22].*.*.[10-22]
* @return bool
*/
function filterRegExpIp($dest_ip, $allow_ip)
{
$dest_arr = explode('.', $dest_ip);
$allow_arr = explode('.', $allow_ip);
for ($i = 0; $i < 4; $i++) {
if ($dest_arr[$i] != $allow_arr[$i]) {
if ($allow_arr[$i] == '*') {
continue;
} else {
preg_match_all("/^\[([\d,\-]*)\]$/", $allow_arr[$i], $match);
$is_element = false;
if (isset($match[1][0]) && $match[1][0] != "") {
$elements = explode(",", $match[1][0]);
foreach ($elements as $k => $element) {
if (preg_match("/\-/", $element)) {
list($start, $end) = explode("-", $element);
if ($dest_arr[$i] >= $start && $dest_arr[$i] <= $end) {
$is_element = true;
break;
}
} elseif ($dest_arr[$i] == $element) {
$is_element = true;
break;
}
}
}
if(!$is_element)
{
return false;
}
}
}
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php