I like to check if an IP address is in a private network. It doesn't work.
My code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
function _isPrivate($ip)
{
$i = explode('.', $ip);
if ($i[0] == 10) {
return true;
} else if ($i[0] == 172 && $i[1] > 15 && $i[1] < 32) {
return true;
} else if ($i[0] == 192 && $i[1] == 168) {
return true;
}
return false;
}
?>
The other one:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
function _isPrivate($ip)
{
$ip = ip2long($ip);
$net_a = ip2long('10.255.255.255') >> 24;
$net_b = ip2long('172.31.255.255') >> 20;
$net_c = ip2long('192.168.255.255') >> 16;
return $ip >> 24 === $net_a || $ip >> 20 === $net_b || $ip >> 16 === $net_c;
}
?>
Any help would be much appreciated, thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…