The purpose of this technote is to provide a small recipe for manipulating and/or converting IP addresses to decimals and back. I wrote a similar technote on how to do this in the Perl language. I thought it would be just as useful to know how to do the same thing in PHP. PHP has a few built-in functions but use of these functions may not yield the desired results. The PHP function ip2long( string ip_address) is a built-in function that will return an integer. PHP’s Integer type is signed, so many IP addresses will result in a negetive number. ip2long Example: < ?php $long_a = ip2long('10.0.0.1'); $long_b= ip2long('192.168.0.1'); ?> Code (markup): In this example, $long_a = -1062731775 and $long_b = 167772161. A good way to resolve this issue of signed vs unsigned is to make your own function to do it for you. The following function and example are shown below: ip2dec function < ?php $long_a = ip2dec('10.0.0.1'); $long_b= ip2dec('192.168.0.1'); function ip2dec($ip) { return (double)(sprintf("%u", ip2long($ip))); } ?> Code (markup): Wow, much different results! $long_a still eqals 167772161. But look at $long_b! $long_b = 3232235521. By the way, this is how the PEAR module Net_IPv4 function Net_IPv4::ip2double does it. The PHP on-line docs also make mention that you may have to use printf or sprintf to get the desired results. 123tweak.com
For what purpose did you post it? i don't see a real question. And the code code, its given by the php page (http://php.net/manual/en/function.ip2long.php) $ip = sprintf("%u", ip2long(long2ip(ip2long($_SERVER['REMOTE_ADDR'])))); PHP: