<?php
function testIP($ip) {
if($ip == '*' || $ip == '*.*.*.*') {
return TRUE;
}
if($_SERVER['REMOTE_ADDR'] == $ip) {
return TRUE;
}
$mask = str_replace('.*', '', $ip);
return strpos($_SERVER['REMOTE_ADDR'], $mask) === 0;
}
$_SERVER['REMOTE_ADDR'] = '70.69.68.67';
$ip = '1.11.1.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '2.34.9.1';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.11.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.69.68.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.69.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '70.*.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '*.*.*.*';
echo "Is $ip good: "; var_dump(testIP($ip));
$ip = '*';
echo "Is $ip good: "; var_dump(testIP($ip));
将输出:
Is 1.11.1.* good: bool(false)
Is 2.34.9.1 good: bool(false)
Is 70.11.*.* good: bool(false)
Is 70.69.68.* good: bool(true)
Is 70.69.*.* good: bool(true)
Is 70.*.*.* good: bool(true)
Is *.*.*.* good: bool(true)
Is * good: bool(true)
如果您要查找特定的 IP(无通配符)检查,则可以使用:
function is_private_ip($ip) {
return filter_var($ip, FILTER_VALIDATE_IP) != FALSE;
}
var_dump(is_private_ip('82.237.3.3'));var_dump(is_private_ip('748.1234.5.4'));