You are here

function ip2country_get_country in IP-based Determination of a Visitor's Country 6

Same name and namespace in other branches
  1. 8 ip2country.module \ip2country_get_country()
  2. 7 ip2country.module \ip2country_get_country()

Gets the ISO 3166 2-character country Code from the IP address.

Parameters

$ip_address: IP address either as a dotted quad string (e.g. "127.0.0.1") or as a 32-bit unsigned long integer.

Return value

FALSE if the lookup failed to find a country for this IP.

5 calls to ip2country_get_country()
ip2countryTestCase::testIPLookup in ./ip2country.test
Tests IP lookup for addresses in / not in the database.
ip2country_admin_settings in ./ip2country.admin.inc
Default IP to Country administration settings.
ip2country_admin_settings_submit in ./ip2country.admin.inc
Processes forms submitted by IP to Country administration page.
ip2country_user in ./ip2country.module
Implements hook_user().
_ip2country_lookup in ./ip2country.admin.inc
AJAX callback to lookup an IP address in the database.

File

./ip2country.module, line 176
Determination of user's Country based on IP.

Code

function ip2country_get_country($ip_address) {
  $ipl = ip2long($ip_address);
  if (is_long($ip_address)) {
    $ipl = $ip_address;
  }

  // Locate IP within range.
  $sql = "SELECT country FROM {ip2country}\n          WHERE (%d >= ip_range_first AND %d <= ip_range_last) LIMIT 1";
  $country_code = db_result(db_query($sql, $ipl, $ipl));
  return $country_code;
}