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


require_once "Net/GeoIP.php";

string lookupCountryName() ( string $addr )


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

require_once "Net/GeoIP.php";

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

try {
} catch (
Exception $e) {
// Handle exception


  • 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:


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

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

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


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.