You are here

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

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

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

Parameters

string|int $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

string|false ISO 3166-1 2-character country code for this IP address, or FALSE if the lookup failed to find a country.

7 calls to ip2country_get_country()
drush_ip2country_lookup in ./ip2country.drush.inc
Implements drush_hook_COMMAND() for the ip2country-lookup command.
ip2countryTestCase::testIpLookup in tests/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_country in ./ip2country.rules.inc
Determines if the user's country is one of the selected countries.

... See full list

File

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

Code

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

  // Locate IP within range.
  $sql = "SELECT country FROM {ip2country}\n             WHERE (:start >= ip_range_first AND :end <= ip_range_last) LIMIT 1";
  $result = db_query($sql, array(
    ':start' => $ipl,
    ':end' => $ipl,
  ))
    ->fetchField();
  return $result;
}