note 23647 added to function.ip2long
| From: | rk at netuse dot de | Date: | Wed, 24 Jul 2002 09:35:49 +0000 |
| Subject: | note 23647 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33478@lists.php.net to get a copy of this message | ||
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;
}
}
--
http://www.php.net/manual/en/function.ip2long.php
http://master.php.net/manage/user-notes.php?action=edit+23647
http://master.php.net/manage/user-notes.php?action=delete+23647
http://master.php.net/manage/user-notes.php?action=reject+23647