Skip to content

PHP – Geolocation

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.

Published inPHP

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.