kieranbarnes Independent PHP, WordPress & CubeCart Programmer

PHP Geo-IP Location in 4 lines

Posted on August 16, 2007

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.

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

<img src="images/flags/'.strtolower($location).'.png" alt="'.$location.'" title="'.$location.'" />

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


Related posts

  1. City & Country Location Finding with GeoIP
    Following on from a previous post, "PHP Geo-IP Location in 4 lines", I've had a...
  2. Auto Updating GeoIP Binary Databases
    Here's how I keep on top of updating the GeoIP binary databases used by various...
  3. mod_geoip Revisted including stopping spam in phpBB 2
    I've recently moved a client's forum onto a new cPanel server, previously it was on...
  4. Yahoo GeoLocation API
    The Yahoo Maps API is the best API I've found for using on in internal...
  5. CubeCart and WordPress Integration
    Following my 100% CubeCart orientated posts, here's another late night hack. It integrates a WordPress...

Tagged as: , , , Leave a comment
Comments (3) Trackbacks (0)
  1. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  2. I think I have just come


Leave a comment

(required)

No trackbacks yet.