Bug #14018: make ip2long and long2ip handle unsigned longs correctly
| From: | jan at kneschke dot de | Date: | Sun, 11 Nov 2001 18:15:58 +0000 |
| Subject: | Bug #14018: make ip2long and long2ip handle unsigned longs correctly | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-70147@lists.php.net to get a copy of this message | ||
From: jan@kneschke.de
Operating system: Linux
PHP version: 4.0.6
PHP Bug Type: Network related
Bug description: make ip2long and long2ip handle unsigned longs correctly
Actually the my fix for this problem is on the PHP side,
not in the PHP-code it self:
function jk_ip2long ($ip) {
preg_match("#^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$#",
$ip, $a);
$long =
$a[1] << 24 |
$a[2] << 16 |
$a[3] << 8 |
$a[4] << 0;
## handling negative urls
if($long < 0) $long += pow(2,32);
return $long;
}
## long2ip
function jk_long2ip ($long) {
## handle long value which should be signed in PHP but
are unsigned
if ($long > 0x7fffffff) {
## although this code seems to be useless, it does
what we want
$l = ($long - 0x7fffffff) - 1;
$long = -(0x7fffffff - $l) - 1;
}
$ip3 = sprintf("%d.%d.%d.%d",
$long >> 24 & 0xff,
$long >> 16 & 0xff,
$long >> 8 & 0xff,
$long >> 0 & 0xff
);
return $ip3;
}
You can see a working example at
http://jan.kneschke.de/projects/phpbugs/ip2long.php
--
Edit bug report at: http://bugs.php.net/?id=14018&edit=1