Net_GeoIP
[ class tree: Net_GeoIP ] [ index: Net_GeoIP ] [ all elements ]

Class: Net_GeoIP

Source Location: /Net_GeoIP-1.0.0RC1/GeoIP.php

Class Overview


GeoIP class provides an API for performing geo-location lookups based on IP address.


Author(s):

Version:

  • $Revision: 1.3 $

Methods


Inherited Variables

Inherited Methods


Class Details

[line 125]
GeoIP class provides an API for performing geo-location lookups based on IP address.

To use this class you must have a [binary version] GeoIP database. There is a free GeoIP country database which can be obtained from Maxmind: http://www.maxmind.com/app/geoip_country

SIMPLE USE

Create an instance:


1 $geoip = Net_GeoIP::getInstance('/path/to/geoipdb.dat', Net_GeoIP::SHARED_MEMORY);

Depending on which database you are using (free, or one of paid versions) you must use appropriate lookup method:


1 // for free country db:
2 $country_name = $geoip->lookupCountryName($_SERVER['REMOTE_ADDR']);
3 $country_code = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
4
5 // for [non-free] region db:
6 list($ctry_code, $region) = $geoip->lookupRegion($_SERVER['REMOTE_ADDR']);
7
8 // for [non-free] city db:
9 $location = $geoip->lookupLocation($_SERVER['REMOTE_ADDR']);
10 print "city: " . $location->city . ", " . $location->region;
11 print "lat: " . $location->latitude . ", long: " . $location->longitude;
12
13 // for organization or ISP db:
14 $org_or_isp_name = $geoip->lookupOrg($_SERVER['REMOTE_ADDR']);

MULTIPLE INSTANCES

You can have several instances of this class, one for each database file you are using. You should use the static getInstance() singleton method to save on overhead of setting up database segments. Note that only one instance is stored per filename, and any flags will be ignored if an instance already exists for the specifiedfilename.

Special note on using SHARED_MEMORY flag

If you are using SHARED_MEMORY (shmop) you can only use SHARED_MEMORY for one (1) instance (i.e. for one database). Any subsequent attempts to instantiate using SHARED_MEMORY will read the same shared memory block already initialized, and therefore will cause problems since the expected database format won't match the database in the shared memory block.

Note that there is no easy way to flag "nice errors" to prevent attempts to create new instances using SHARED_MEMORY flag and it is also not posible (in a safe way) to allow new instances to overwrite the shared memory block.

In short, is you are using multiple databses, use the SHARED_MEMORY flag with care.

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:


1 $geoip->lookupCountryName(gethostbyname('www.sunset.se'));

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


1 if (ip2long($ip_or_name) === false) {
2 $ip = gethostbyname($ip_or_name);
3 } else {
4 $ip = $ip_or_name;
5 }
6 $ctry = $geoip->lookupCountryName($ip);



[ Top ]


Method Detail

close   [line 514]

int close( )

Closes the geoip database.
  • Return: Status of close command.

[ Top ]

getInstance   [line 361]

void getInstance( [string $filename = null], [int $flags = null])

Singleton method, use this to get an instance and avoid re-parsing the db.

Unique instances are instantiated based on the filename of the db. The flags are ignored -- in that requests to for instance with same filename but different flags will return the already-instantiated instance. For example:


1 // create new instance with memory_cache enabled
2 $geoip = Net_GeoIP::getInstance('C:\mydb.dat', Net_GeoIP::MEMORY_CACHE);
3 ....
4
5 // later in code, request instance with no flags specified.
6 $geoip = Net_GeoIP::getInstance('C:\mydb.dat');
7
8 // Normally this means no MEMORY_CACHE but since an instance
9 // with memory cache enabled has already been created for 'C:\mydb.dat', the
10 // existing instance (with memory cache) will be returned.

NOTE: You can only use SHARED_MEMORY flag for one instance! Any subsquent instances that attempt to use the SHARED_MEMORY will use the *same* shared memory, which will break your script.


Parameters:

string   $filename     
int   $flags     Flags that control class behavior.
  • Net_GeoIp::SHARED_MEMORY - use SHMOP to share a db among multiple PHP instances. NOTE: ONLY ONE GEOIP INSTANCE CAN USE SHARED MEMORY!!!
  • Net_GeoIp::MEMORY_CACHE - store the full contents of the database in memory for current script. This is useful if you access the database several times in a script.
  • Net_GeoIp::STANDARD - [default] standard no-cache version.

[ Top ]

getOrg   [line 679]

void getOrg( int $ipnum)

Seek and return organization (or ISP) name for converted IP addr.
  • Todo: -cGeoIP Consider adding MEMORY_CACHE support to the getOrg() method (if there is a perf. difference).

Parameters:

int   $ipnum     Converted IP address.

[ Top ]

getRecord   [line 739]

Net_GeoIP_Location getRecord( int $ipnum)

Seek and populate Net_GeoIP_Location object for converted IP addr.

Note: this


Parameters:

int   $ipnum     Converted IP address.

[ Top ]

getRegion   [line 701]

array getRegion( int $ipnum)

Seek and return the region info (array containing country code and region name) for converted IP addr.
  • Return: Array containing country code and region: array($country_code, $region)

Parameters:

int   $ipnum     Converted IP address.

[ Top ]

loadSharedMemory   [line 408]

void loadSharedMemory( string $filename)

Loads the database file into shared memory.
  • Throws: Exception - if unable to read the db file.

Parameters:

string   $filename     Path to database file to read into shared memory.

[ Top ]

lookupCountryCode   [line 556]

string lookupCountryCode( string $addr)

Returns 2-letter country code (e.g. 'CA') for specified IP address.

Use this method if you have a Country database.


Parameters:

string   $addr     IP address (hostname not allowed).

[ Top ]

lookupCountryId   [line 536]

void lookupCountryId( string $addr)

Get the country index.

This method is called by the lookupCountryCode() and lookupCountryName() methods. It lookups up the index ('id') for the country which is the key for the code and name.

  • Throws: Exception - if IP address is invalid.
    • if database type is incorrect

Parameters:

string   $addr     

[ Top ]

lookupCountryName   [line 569]

string lookupCountryName( string $addr)

Returns full country name for specified IP address.

Use this method if you have a Country database.


Parameters:

string   $addr     IP address (hostname not allowed).

[ Top ]

lookupLocation   [line 661]

Net_GeoIP_Location lookupLocation( string $addr)

Lookup the location record for given IP address.

Use this method if you have a City database.

  • Return: The full location record.
  • Throws: Exception - if IP address is invalid.

Parameters:

string   $addr     IP address (hostname not allowed).

[ Top ]

lookupOrg   [line 623]

void lookupOrg( string $addr)

Lookup the organization (or ISP) for given IP address.

Use this method if you have an Organization/ISP database.

  • Throws: Exception - if IP address is invalid.
    • if database is of wrong type

Parameters:

string   $addr     IP address (hostname not allowed).

[ Top ]

lookupRegion   [line 642]

array lookupRegion( string $addr)

Lookup the region for given IP address.

Use this method if you have a Region database.

  • Return: Array containing country code and region: array($country_code, $region)
  • Throws: Exception - if IP address is invalid.

Parameters:

string   $addr     IP address (hostname not allowed).

[ Top ]

open   [line 375]

void open( string $filename, [int $flags = null])

Opens geoip database at filename and with specified flags.
  • Throws: Exception - if unable to open specified file or shared memory.

Parameters:

string   $filename     
int   $flags     

[ Top ]

seekCountry   [line 581]

int seekCountry( int $ipnum)

Using the record length and appropriate start points, seek to the country that corresponds to the converted IP address integer.
  • Return: Offset of start of record.
  • Throws: Exception - if fseek() fails on the file or no results after traversing the database (indicating corrupt db).

Parameters:

int   $ipnum     Result of ip2long() conversion.

[ Top ]

setupSegments   [line 433]

void setupSegments( )

Parses the database file to determine what kind of database is being used and setup segment sizes and start points that will be used by the seek*() methods later.

[ Top ]


Documentation generated on Fri, 01 Sep 2006 09:15:05 -0400 by phpDocumentor 1.2.3. PEAR Logo Copyright © PHP Group 2004.