<?php
// 限制域名测试
$domain = ‘2.baidu.com’;
$domain_list = ‘*.baidu.com,qq.com’;
if (in_host ( $domain, $domain_list )) {
echo (‘domain in’);
} else {
echo (‘domain is not in’);
}
// 限制IP测试
$host = ‘127.1.1.88’;
$list = ‘127.0.0.,192.168.1.1,192.168.1.70,127.1.1.33-127.1.1.100’;
if (in_host ( $host, $list )) {
echo (‘ip in’);
} else {
echo (‘ip is not in’);
}
/*
- HOST访问限制 支持 IP(单IP,多IP,通配符,IP段) 域名(单域名,多域名,通配符)
- 根据判断实现IP地址 白名单黑名单
- Author:@Aler
- @param unknown $host 当前host 127.0.0.2
- @param unknown $list 允许的host列表 127.0.0.*,192.168.1.1,192.168.1.70,127.1.1.33-127.1.1.100
- @return boolean
/
function in_host($host, $list) {
$list = ‘,’ . $list . ‘,’;
$is_in = false;
// 1.判断最简单的情况
$is_in = strpos ( $list, ‘,’ . $host . ‘,’ ) === false ? false : true;
// 2.判断通配符情况
if (! $is_in && strpos ( $list, ‘’ ) !== false) {
$hosts = array ();
$hosts = explode ( ‘.’, $host );
// 组装每个 * 通配符的情况
foreach ( $hosts as $k1 => $v1 ) {
$host_now = ”;
foreach ( $hosts as $k2 => $v2 ) {
$host_now .= ($k2 == $k1 ? ‘*’ : $v2) . ‘.’;
}
// 组装好后进行判断
if (strpos ( $list, ‘,’ . substr ( $host_now, 0, – 1 ) . ‘,’ ) !== false) {
$is_in = true;
break;
}
}
}
// 3.判断IP段限制
if (! $is_in && strpos ( $list, ‘-‘ ) !== false) {
$lists = explode ( ‘,’, trim ( $list, ‘,’ ) );
$host_long = ip2long ( $host );
foreach ( $lists as $k => $v ) {
if (strpos ( $v, ‘-‘ ) !== false) {
list ( $host1, $host2 ) = explode ( ‘-‘, $v );
if ($host_long >= ip2long ( $host1 ) && $host_long <= ip2long ( $host2 )) {
$is_in = true;
break;
}
}
}
}
return $is_in;
}
网友评论