PHP Geo-IP Location in 4 lines

August 16th, 2007
No Gravatar

Want to look up IP addresses to a geographical location? Firstly, download maxmind_geoip.php and GeoIP binary database from MaxMind. I copied them to a universal location on a server so I can use it across applications.

1
2
3
4
5
6
7
require_once '/usr/local/share-php/maxmind_geoip.php';
 
$gi = geoip_open("/usr/local/share-php/GeoIP.dat",GEOIP_STANDARD);
 
$location = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
 
geoip_close($gi);

There we go 4 lines.

$location will return the 2 letter ISO country code for that country, for you to do whatever you want with!

Why not combine it with the Flag Icon Set from Fam Fam Fam? Here’s how I did it

1
&ltimg src="images/flags/'.strtolower($location).'.png" alt="'.$location.'" title="'.$location.'" /&gt

You could even use the PECL extenstion or the PEAR extenstion if you’re feeling fancy.

Bookmark it del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Propeller | Yahoo


Was this post useful to you? Let me know, buy me a beer!
Alternatively, if you're feeling impecunious, you may like to subscribe to my RSS feed, or see other articles in the PHP category.

Leave a Reply