public function Ip2CountryLookup::getCountry in IP-based Determination of a Visitor's Country 8
Gets the ISO 3166 2-character country code from the IP address.
Parameters
string|int|null $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.
Overrides Ip2CountryLookupInterface::getCountry
File
- src/
Ip2CountryLookup.php, line 43
Class
- Ip2CountryLookup
- The ip2country.lookup service.
Namespace
Drupal\ip2countryCode
public function getCountry($ip_address = NULL) {
$ip_address = isset($ip_address) ? $ip_address : $this->currentRequest
->getClientIp();
$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 = $this->connection
->query($sql, [
':start' => $ipl,
':end' => $ipl,
])
->fetchField();
return $result;
}