PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Networking » Net_GeoIP » Manual

Library to perform geo-location lookups of IP addresses. This package is built on top of MaxMind's GeoIP databases to accurately determine the geographic location of an IP address. MaxMind offers both free and non-free database. Please refer to their website for specific instructures on obtaining both database types.

Net_GeoIP::getInstance()

Net_GeoIP::getInstance() – method to get an instance and avoid re-parsing the database

Synopsis

require_once "Net/GeoIP.php";

object getInstance() ( string $filename [, int $flags] )

Description

This method is an implementation of the so-called singleton pattern and is the preferred way to create instances of Net_GeoIP.

Creating an instance of Net_GeoIP

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

$geoip = Net_GeoIP::getInstance("/path/to/geoipdb.dat", Net_GeoIP::SHARED_MEMORY);
?>

Multiple Instances

If you want to use multiple databases in one application, you will need to create an instance of Net_GeoIP for each database. Using the singleton getInstance() method will make sure that at any given point exactly one object for each database exists, which saves on overhead of setting up database segments.

Parameter

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

Throws

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

Net_GeoIP::lookupCountryCode()

Net_GeoIP::lookupCountryCode() – returns 2-letter country code (e.g. "CA") for specified IP address

Synopsis

require_once "Net/GeoIP.php";

string lookupCountryCode() ( string $addr )

Description

This method returns the 2-letter country code for the given IP address. It works with both the free and the non-free databases.

Looking up the country code

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

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

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

Parameter

Throws

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

Net_GeoIP::lookupRegion()

Net_GeoIP::lookupRegion() – returns the region for given IP address.

Synopsis

require_once "Net/GeoIP.php";

array lookupRegion() ( string $addr )

Description

This method returns an array containing the country code and the region for the specified IP address. It works only with a non-free Region database.

Looking up the region

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

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

try {
    list($country_code, $region) = $geoip->lookupRegion($_SERVER['REMOTE_ADDR']);
} catch (Exception $e) {
    // Handle exception
}
?>

Parameter

Throws

This method throws an exception if the IP address is invalid.

Net_GeoIP::lookupLocation()

Net_GeoIP::lookupLocation() – returns the location record for specified IP address

Synopsis

require_once "Net/GeoIP.php";

object lookupLocation() ( string $addr )

Description

This method returns an instance of Net_GeoIP_Location for the specified IP address. It works only with a non-free City database.

Looking up the location record

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

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

try {
    $location = $geoip->lookupLocation($_SERVER['REMOTE_ADDR']);

    var_dump($location);

    printf("City: %s, %s\nLatitude: %s, Longitude: %s\n",
           $location->city, $location->region,
           $location->latitude, $location->longitude);

} catch (Exception $e) {
    // Handle exception
}
?>

Parameter

Throws

This method throws an exception if the IP address is invalid.

Net_GeoIP::lookupOrg()

Net_GeoIP::lookupOrg() – returns the name of the organization or ISP for the given IP address.

Synopsis

require_once "Net/GeoIP.php";

string lookupOrg() ( string $addr )

Description

This method returns the name of the organization or of the ISP which has registered the IP address range that contains the specified IP address. It works only with a non-free Organization/ISP database.

Looking up organization name

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

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

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

Parameter

Throws

This method throws an exception if the IP address is invalid or the database type is wrong.