Implementing Free Geolocation with PHP Using Maxmind’s GeoLite Country
Need to serve up different content for people from different countries? Or track users by country? Or make a site more usable by providing options relevant to a particular country? I recently had a small programming task which required pre-selection of a country field based on the visitor’s country.
Maxmind offers a free Geolocation database, GeoLite Country which allows lookup of country from an IP. They claim it to be 99.3% accurate. They have a paid version that is more accurate. They also offer other databases, including one to lookup a city from an IP. The database is available as CSV data or in binary format. The binary format is very easy to implement as they provide APIs in a variety of languages. I used their PHP API. There are code samples to get things going quickly.
I used their PHP class which was the easiest to implement. Simply upload the class file and binary file and require the class file. The calls are simple and the lookup is fast. Very easy to implement.
November 6th, 2008 at 3:11 am
You can also use the PHP API from http://www.ip2location.com/php.aspx which is free with IPv6 support.