Re: Fwd: Re: [PHP] Unable to get a dot-decimal ip address from a decimal ip address
| From: | Tom Rogers | Date: | Sat, 06 Oct 2001 07:54:10 +0000 |
| Subject: | Re: Fwd: Re: [PHP] Unable to get a dot-decimal ip address from a decimal ip address | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70209@lists.php.net to get a copy of this message | ||
Hi
Here are a couple of functions that might help.
<?
function iptonum($ip){
$split = explode(".",$ip);
return (16777216 * $split[0]) + (65536 * $split[1]) + (256 * $split[2]) + $split[3];
}
function numtoip($num){
$ip = "";
$y = 16777216;
for($x = 0;$x < 3;$x++){
$a = $num / $y;
$b = intval($a);
$num = ($a - $b) * $y;
$ip .= $b.".";
$y = $y >> 8;
}
$ip .= $num;
return $ip;
}
?>
Tom
At 09:48 PM 4/10/01, dharana@dharana.net wrote:
----- Forwarded message from dharana@dharana.net ----- From: dharana@dharana.net To: David Robley <huntsman@www.nisu.flinders.edu.au> Date: Thu, 4 Oct 2001 13:37:07 +0200 Subject: Re: [PHP] Unable to get a dot-decimal ip address from a decimal ip address Something weird is going on: PHP translatesthis into this3232235524 127.255.255.255 3579939092 127.255.255.255 3579949354 127.255.255.255 3232235524 127.255.255.255 3237340682 127.255.255.255 but then.. 1042595228 62.36.189.156 Is it just my PHP installation that is corrupt or does long2ip has the same limitations as decbin()? PHP Version 4.0.6 compilled from sources into a linux 2.4.10 box. David Robley(huntsman@www.nisu.flinders.edu.au)@2001.10.04 14:09:33 +0000: On Thu, 4 Oct 2001 13:10, dharana@dharana.net wrote:Hi, decbin() is unable to handle decimal numers greater than 2147483647 and it's a problem. That's in the docs, ok. The situation is: I have a list of IPs in decimal format (ex: 3232235524) and I wanted to translate them into dot decimal. As far as I know the formula is: [...]by converting the number to binary, breaking the binary number down into 4 octets and then converting each octet into a dotted-decimal number. (got from http://www.montagar.com/~patj/ipmath.txt) When I try to do this with PHP, I get just 1's because the IPs are greater than 2147483647. Anyone knows a workaround for this? Thanks in advance.I think the function ip2long might be what you want. --David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIAKeyboard Not Found - Press [F1] to Continue --dharana <dharana@dharana.net> Black holes are where PGP KeyID: 0xC32A724 GOOD is dividing by zero----- End forwarded message ----- --dharana <dharana@dharana.net> Black holes are where PGP KeyID: 0xC32A724 GOOD is dividing by zero