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 - http://maxmind.com/.

 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 http://maxmind.com/.


 Requirements:

 GeoIP C Library/API (http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz)
 PHP (will commonly work on version 5.x)
 PEAR/PECL
 re2c


 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 http://php.net/, for PEAR/PECL go to http://pear.php.net/manual/en/installation.getting.php.


 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 http://re2c.org/ if you're interested with the source.

 wget http://packages.sw.be/re2c/re2c-0.13.5-1.el5.rf.x86_64.rpm
 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.

 wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
 tar -xzvf GeoIP.tar.gz

 cd GeoIP-1.4.6/
 ./configure
 make
 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 "extension=geoip.so" 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 http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
 gunzip GeoLiteCity.dat.tar.gz
 mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat

Maintenance and Updates

If maxmind.com 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 http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
        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 http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
        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("yahoo.co.uk");
print_r(geoip_record_by_name("magtanong.com"));

More functions here - http://www.php.net/manual/en/ref.geoip.php

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: