Wednesday, October 20, 2010

GeoIP PHP Module - A Free GeoLocation Database with a High-Performance API

This product and it's database is created and updated by Maxmind -

 Usage Recommendations:

 1) If you want to check geolocation-related information per IP and you expect a load of users to use it simulataneously (the API can do as much as 400,000 requests/sec).
 2) If data accuracy is acceptable at 99.5%.
 3) If a monthly data update is acceptable.
 4) Not to be used as a spreadsheet, tabular report since it can't be.
 5) Each request returns a single record.

 Then, this is a solution.

 If you want it to be updated automatically, more frequently and has a license to it to all our servers, then you should get the commercial versions from


 GeoIP C Library/API (
 PHP (will commonly work on version 5.x)

 How to Install:

 For the following instructions, it is assumed that you have PHP and PEAR/PECL package manager already installed. For instructions on how to get PHP go to, for PEAR/PECL go to

 cd /usr/local/src

 1) Check if the re2c tool is present.

 which re2c

 2) if re2c is not present, download and install it. You can also alternatively look for other sources by a quick google search. You can go to if you're interested with the source.

 rpm -ivUh re2c-0.13.5-1.el5.rf.x86_64.rpm

 3) If it is already installed, proceed with installing the GeoIP C API.

 tar -xzvf GeoIP.tar.gz

 cd GeoIP-1.4.6/
 make check
 make install

 4) Install geoip via PECL

 pecl install geoip

 5) Activate the PHP Extension, so you can start using it's functions.

 vi /etc/php.d/geoip.ini

 then paste "" and save.

 6) The GeoIP C API/Library only comes with country database, to get city information, let's also mount the GeoCityLite database.

 wget -N
 gunzip GeoLiteCity.dat.tar.gz
 mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

Maintenance and Updates

If published some updates, you can download the database binaries and save them to your database install directory. You can overwrite and/or much better, backup the old files before copying.

1) /usr/local/share/GeoIP/GeoIP.dat (this location is common for RHEL5 and CentOS, might vary with other OS)        - for GeoCountryLite
        cd /your/download/path
        wget -N
        gunzip GeoIP.dat.gz
        mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat
2) /usr/local/share/GeoIP/GeoIPCity.dat (this location is common for RHEL5 and CentOS, might vary with other OS)    - for GeoCityLite

        cd /your/download/path
        wget -N
        gunzip GeoLiteCity.dat.gz
        mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

Sample Code

php -a #(PHP Interactive Shell)
echo geoip_country_code_by_name("");

More functions here -

If you don't get PHP errors and it's returning data, then your installation should be working.

  • Related Links Widget for Blogspot

No comments: