Net_GeoIP::lookupCountryName()

Net_GeoIP::lookupCountryName() – returns full country name for specified IP address

Synopsis

require_once "Net/GeoIP.php";

string lookupCountryName() ( string $addr )

Description

This method returns the full country name for the given IP address. It works with both the free and the non-free databases.

Looking up the country name

<?php
require_once "Net/GeoIP.php";

$geoip Net_GeoIP::getInstance("/path/to/geoipdb.dat");

try {
    echo 
$geoip->lookupCountryName($_SERVER['REMOTE_ADDR']);
} catch (
Exception $e) {
    
// Handle exception
}
?>

Parameter

  • string $addr - IP address

    Lookups on Hostnames

    Note that this PHP API does NOT support lookups on hostnames. This is so that the public API can be kept simple and so that the lookup functions don't need to try name lookups if IP lookup fails (which would be the only way to keep the API simple and support name-based lookups).

    If you do not know the IP address, you can convert an name to IP very simply using PHP native functions or other libraries:

    <?php
    $geoip
    ->lookupCountryName(gethostbyname("example.com"));
    ?>

    Or, if you don't know whether an address is a name or IP address, use application-level logic:

    <?php
    if (ip2long($ip_or_name) === false) {
        
    $ip gethostbyname($ip_or_name);
    } else {
        
    $ip $ip_or_name;
    }

    $country $geoip->lookupCountryName($ip);
    ?>

Throws

This method sthrow an exception if the IP address is invalid or if the database type is incorrect.

method to get an instance and avoid re-parsing the database (Previous) returns 2-letter country code (e.g. CA) for specified IP address (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.