/**
* 安全IP检测,支持IP段检测
* @param string $ip 要检测的IP
* @param string|array $ips 白名单IP或者黑名单IP
* @return boolean true 在白名单或者黑名单中,否则不在
*/
function is_safe_ip($ip="",$ips=""){
if(!$ip) $ip = get_client_ip(); //获取客户端IP
if($ips){
if(is_string($ips)){ //ip用"," 例如白名单IP:192.168.1.1,123.23.23.1,193.134.*.*
$ips = explode(",", $ips);
}
}else{ //读取后台配置 白名单IP
$obj = new Setting();
$ips = explode(",", $obj->getConfig("whiteip"));
}
if(in_array($ip, $ips)){
return true;
}
$ipregexp = implode('|', str_replace( array('*','.'), array('\d+','\.') ,$ips));
$rs = preg_match("/^(".$ipregexp.")$/", $ip);
if($rs) return true;
return ;
}
function get_client_ip() {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}