php: ip2long returning negative val

ip, php

Solution

glopes@nebm:~$ php -r "printf('%u', -931792879);"
3363174417

There you go. My guess is that you are on a system with 32-bit ints and your `ip_address_to_number` is actually returning a float.

You see, with 32-bit ints, your maximum positive integer is `(2^31) - 1 = 2 147 483 647`, so the integer wraps around.

If you want to mimic the behaviour of the PHP function, do:

function ip_address_to_number($IPaddress) { 
 if(!$IPaddress) {
  return false;
 } else {
  $ips = split('\.',$IPaddress);
  return($ips[3] | $ips[2] << 8 | $ips[1] << 16 | $ips[0] << 24);
 }
}

(by the way, `split` has been deprecated)

Problem

``` function ip_address_to_number($IPaddress) { if(!$IPaddress) { return false; } else { $ips = split('\.',$IPaddress); return($ips[3] + $ips[2]*256 + $ips[1]*65536 + $ips[0]*16777216); } } ``` that function executes the same code as the php bundled function ip2long. however, when i print these 2 values, i get 2 different returns. why? (im using php 5.2.10 on a wamp environment). ``` ip2long('200.117.248.17'); //returns **-931792879** ip_address_to_number('200.117.248.17'); // returns **3363174417** ``` Applied and continued here: Showing my country based on my IP, mysql optimized

Original source