IP-numbers not found in database table ip2country I am trying to hide a certain field in the product nodes for all visitors from other countries. I have installed the ip2country module and I am using this code snippet to fetch the language code in my module:

$ip = ip_address();
$country_code = get_country($ip);       
dsm($ip . " " . $country_code);


The IP address is showing but not the country code. I am testing this on a web server and the IP printed by the dsm seems OK. I have also copied and renamed the ip2country_get_country($ip_address) function from the ip2 country module to experiment with different solutions in my own module:

function get_country($ip_address) {
  $ipl = ip2long($ip_address);
  if (is_long($ip_address)) {
    $ipl = $ip_address;
  }
dsm($ipl);
  // Locate IP within range.
  $sql    = "SELECT country FROM {ip2country}
             WHERE (:start &gt;= ip_range_first AND :end &lt;= ip_range_last) LIMIT 1";
  $result = db_query($sql, array(':start' =&gt; $ipl, ':end' =&gt; $ipl))-&gt;fetchField();

  return $result;
}


The thing is that the long ip produced by the get_country() function ($ipl = ip2long($ip_address)) is ten numbers, and as far as I can see in the database table no numbers for my country (Sweden) is made up of ten numbers... So it's not strange that the db query does not return the expected result ('SE'). There must be something obvious I am missing here... ? I have experimented with SQL queries directly in phpMyAdmin and I can confirm that the IP number is not to be found in any of the ranges given.
The comment from AyeshK led me to the right answer. I found this in the php manual:


  As pointed out by others, this function's return value is not
  consistent on 32 and 64-bit platforms.
  
  Your best bet for consistent behavior across 32/64-bit platforms (and
  Windows) is the lowest common denominator: force the return value of
  ip2long() into a signed 32-bit integer, e.g.:

var_dump(unpack('l', pack('l', ip2long('255.255.255.0'))));



And so I changed this row $ipl = ip2long($ip_address); into this $ipl = unpack('l', pack('l', ip2long($ip_address))); and now it works!

But if there is a better solution, please let me know.