note 23647 deleted from function.ip2long by sniper
| From: | sniper@php.net | Date: | Sat, 20 Sep 2003 07:49:49 +0000 |
| Subject: | note 23647 deleted from function.ip2long by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-56798@lists.php.net to get a copy of this message | ||
Note Submitter: rk@netuse.de
----
The given IP_match simply does not work.
Since php does only 32-bit integer, that
whole things breaks on most network data. One need to switch to bcmath:
function IP_match($network, $mask, $ip) {
bcscale(3);
$ip_long=ip2long($ip);
$mask_long=ip2long($network);
#
# Convert mask to divider
#
if (ereg("^[0-9]+$",$mask)) {
/// 212.50.13.0/27 style mask (Cisco style)
$divider=bcpow(2,(32-$mask));
} else {
/// 212.50.13.0/255.255.255.0 style mask
$xmask=ip2long($mask);
if ( $xmask < 0 ) $xmask=bcadd(bcpow(2,32),$xmask);
$divider=bcsub(bcpow(2,32),$xmask);
}
#
# Test is IP within specified mask
#
if ( floor(bcdiv($ip_long,$divider)) == floor(bcdiv($mask_long,$divider)) ){
# match - this IP is within specified mask
return true;
} else {
# fail - this IP is NOT within specified mask
return false;
}
}