Tim Igoe's Web Design, Development and Hosting Blog

PHP – Geolocation

Posted on by Tim Igoe

Need to know the location of your site users to target some specific data at them? Geolocation is the answer, find out the location of the IP Address that that user is coming from. This tutorial uses Geoip technology from Maxmind. Download geoip.inc from http://www.maxmind.com/download/geoip/api/php/geoip.inc and the free country database from http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz Once you have these bits, its easy to use, require('geoip.inc'); $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD); $CountryCode = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); This code will put the international standard 2 character country code into the variable $CountyCode ready for use; While this does work most of the time, it can be succeptable to problems with some users bouncing through proxy servers. This effects only a small minority.

This entry was posted in PHP and tagged , . Bookmark the permalink.

Leave a Reply