Skip to main content

Introduction to GeoIP

In case you’ve ever used Google Analytics or any simmilar tool where you can see exact location from where visitors of your site came from, you’ve probably wondered how they were able to dig that info. Is it magic or what? Of course, it’s not kind of magic, exact location of visitor is defined by visitor’s IP address, and technology used to locate user by his IP is well known as GeoIP.

Today we are going to look how to locate visitor of your site with PHP and Max Mind’s GeoIP database. In examples bellow we used free(lite) versions of GeoIP databases, because fully supported GeoIP databases are not free(you’d have to pay $50USD setup + $12USD update for GeoIP Country and $370USD + $90USD for GeoIP City base). Drawback of lite version is that it is not as accurate as fully supported GeoIP databases, but it is still very usefull and probably good enough for great majority of live projects.

MaxMind offer API for dozen of programming languages (full list is available here), details about PHP API are available here. This tutorial deal with so called “Pure PHP API”, there are also PECL extensions and apache mod_geoip modul available. Apache modul provide better perfomance, but Pure PHP API is easier to set up.

Just for a start let’s download all PHP API files from http://www.maxmind.com/download/geoip/api/php/, and save them somewhere inside of your Web tree(let say /htdocs/geoip). To use GeoIP Country you need to download lite database from here, and for GeoLiteCity download database from here. Just for the sake of simplicity, we are going to unpack both bases to the same dir where we saved our PHP API’s files (/htods/geoip in our example).

GeoIP Country
——————————–
Now, let’s see how country detection works:

<?php
/**
 * GeoIP Country Database Example
 *
 * @version $Id$
 * @package geoip
 * @copyright © 2006 Lampix.net
 * @author Dragan Dinic <dinke@lampix.net>
 */

require_once("geoip.inc");

$gi = geoip_open("GeoIP.dat", GEOIP_STANDARD);

$ip = $_SERVER['REMOTE_ADDR'];
//if you test on localhost use IP bellow for test
//since $_SERVER['REMOTE_ADDR'] would be 127.0.0.1
//$ip = "89.216.226.174";

$country_name = geoip_country_name_by_addr($gi, $ip);
$country_code = geoip_country_code_by_addr($gi, $ip);
if($country_name)
{
	echo "Your country is: $country_name <br />";
	echo "Country Code is: $country_code <br />";
}
else
{
	echo "Sorry, we weren't able to locate you.";
}

geoip_close($gi);
?>

So, at the beggining we’ve included geoip.inc which contains all functions needed to use GeoIP country database, then we’ve created new instance of GeoIP class with geoip_open function, and at the end we called proper functions(geoip_country_name_by_addr and geoip_country_code_by_addr) to get country name/code in which detected IP address reside. Again, in case you test localy, don’t use $_SERVER[‘REMOTE_ADDR’].

When you run script above you should get something like this as output:

Your country is: Serbia and Montenegro
Country Code is: CS

GeoIP City
—————————-
Now, let’s extend visitor’s country data with exact location(like city, postal code etc.)

<?php
/**
 * GeoIP City Database Example
 *
 * @version $Id$
 * @package geoip
 * @copyright © 2006 Lampix.net
 * @author Dragan Dinic <dinke@lampix.net>
 */

require_once("geoipcity.inc");

$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);

$ip = $_SERVER['REMOTE_ADDR'];
//if you test on localhost use IP bellow for test
//since $_SERVER['REMOTE_ADDR'] would be 127.0.0.1
//$ip = "89.216.226.174";

$record = geoip_record_by_addr($gi, $ip);

if(!$record)
{
	echo "Sorry, we weren't able to locate you.";
}
else
{
	echo "Country: " .$record->country_name . "<br />";
	echo "Country Code: " . $record->country_code . "<br />";
	echo "Country Code 2: " . $record->country_code3 . "<br />";
	echo "Region: " .$record->region . "<br />";
	echo "City: " .$record->city . "<br />";
	echo "Postal Code: " .$record->postal_code . "<br />";
	echo "Latitude: " .$record->latitude . "<br />";
	echo "Longitude: " .$record->longitude . "<br />";
	echo "DMA Code: " .$record->dma_code . "<br />";
	echo "Area Code: " .$record->area_code . "<br />";
}

geoip_close($gi);
?>

As you see, PHP code is simmilar as in our country detection example, with exception that we used geoipcity.inc and GeoLiteCity.dat database. Function geoip_record_by_addr($gi, $ip) return instance of ‘geoiprecord’ class which contains in it’s properties location’s data we used in our example. After you run script you should get output like this one:

Country: Serbia and Montenegro
Country Code: CS
Country Code 2: SCG
Region: 02
City: Beograd
Postal Code: 11000
Latitude: 44.8186
Longitude: 20.4681
DMA Code:
Area Code:

CaseStudy – Redirection depending of Country
————————————————————–
At the end, we are going to see some real GeoIP usage. Our goal is to redirect users on multy language site(blog) to proper language section on the site depending of their location. Here is how code looks like on my own blog:

<?php
/**
 * Case Study - GeoIP Redirection
 *
 * @version $Id$
 * @package geoip
 * @copyright © 2006 Lampix.net
 * @author Dragan Dinic <dinke@lampix.net>
 */

require_once("geoip/geoip.inc");

$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

geoip_close($gi);

if($country_code == 'CS')
{
        header("HTTP/1.1 301 Moved Permanently");
        header('Location: http://www.dinke.net/blog/sr/');
}
else
{
        header("HTTP/1.1 301 Moved Permanently");
        header('Location: http://www.dinke.net/blog/en/');
}
?>

Above example is used on this blog in order to redirect all users located out of Serbia to english version of the blog. Sending custom 301 redirection headers is important so bots(like google etc. google) are able to index blog pages without problems.

33 thoughts to “Introduction to GeoIP”

  1. Great Tutorial.
    I was ready to hurt someone as i could just not get it working.

    Read the your tut and 2 mins later i have a GeoIP redirecting.

    Thx

  2. dinke, thanks for this great tutorial. but now, because i’m a newbie, so i don’t know how to figure out to detect commenter’s city, flag and country in my wordpress blog’s comment area. i just know the wordpress’s php function to detect/displaying commenter’s ip using $comment->comment_author_IP or comment_author_IP !

    i wanna the output like this one:

    comment from: city flag country

    i’ve asked in wordpress support forum, but there’s no people know to do that. once again, i’m a newbie, so please be patient. 🙂 please tell me the way.

  3. Pingback: geo cities
  4. The Redirection depending of Country does not work :S
    It redirects all surfers depending the country they are.
    Anyone knows why?

  5. Redirection works just fine for me, I am on SBB Network. Either your ISP IP is not listed in free db or you’re gengo cookie (plugin used for dual language blog) is set to english so you are always redirected to english version.

  6. Geoip country works for me, but i cant get geolite city to work no matter what i do. I have all the required files for it to work.

  7. Thank yo for this tutorial! Work Great!

    There is only one thing. Is it possible to display the full name of the states instead of the 2 first letter of the state?

    For instance: It’s the script displays “CA” instead of “California” or it displays “FL” instead of “Florida”

    Any way to fix that?

  8. @wasabi
    Not sure about that, but you can always write an array map with all us states like:

    $states[‘FL’] = ‘Florida’;
    $states[‘CA’] = ‘California’;

    and then get full name from it 🙂

  9. OMG, I love you… been trying to set up a redirect for 3 days…with you, it worked like a charm! I love youuuuuuuu! Thanksssssss! :worship:

  10. just a quick question. I want to accept onyl visitors from 2 countries. How do I add another county?

    This doesn’t work :

    if($country_code == ‘CS’ || ‘FR’)

    Thanks!

  11. Of course it doesn’t 🙂

    Your code should be like this:

    if($country_code == ‘CS’ || $country_code == ‘FR’)

    Or if you have bigger list you can do arrays like:

    $country_list = array(‘CS’, ‘FR’, ‘US’);
    if(in_array($country_code, $country_list))
    {
    //do redirection or whatever you plan to do
    }

  12. Dinke, you’re my hero! I refreshed your blog page every 5 min for your reply! Thank you! Twice! And twice again. Bookmarked! Keep up the great work!

  13. If you are doing city geoip detection in non-english web sites, you may have issues with city name spelling. For Beograd in Serbia, geoip_record_by_addr() will return Beograd, but for some ISPs it is Belgrade or even Novi Beograd.

  14. Thanks for great tutorial. Really amazing.

    Using the redirection of depending country,
    Im redirecting visitor in Canada and other countries,
    But Im stayed in Canada.

    How to allow my ip address and 2-3 ip address in the using this code.

    Tried some codes.. Not works.

    Could you tell me the way solve this.

    Thank you for great code !!!!!!!

  15. Loooooovvvvveeeeeeeeeeeeeeeeeee yyyyoooouuuuuu!!!!!!!!!!
    I was about to throw my computer out the window until I found this!

    I just have a question, because I’m a noob. I have multiple websites and I want to use this for all of them from the same location of the api. For example:
    If I place the ‘geoip’ folder in this location “/public_html/website-no-1” and I have the ‘redirect.php’ in the same location, it works great.
    But if I place the ‘geoip’ folder in “/public_html”, so that I can use it for ‘website-no-1’ and ‘website-no-2’ I get an error. I’m sure it is because of this line:
    require_once(“geoip/geoip.inc”);
    but I can’t figure out what to write there instead of ‘geoip’. I’ve tried ‘/public_html/geoip’ and it doesn’t work. Please help.

  16. How to redirect users based upon state. For e.g i want visitors from California to land in one.php, Florida to two.php and rest all to default.php

  17. This script works a charm. I’d like to use it so that if somebody enters the site at any point, they will be directed to the equivalent url on the target site. For example, if they enter on xyz.com/ipods, they will be redirected to xyz.com/redirect/ipods. If xyz.com/games, they will go to xyz.com/redirect/games. How can I use relative urls to accomplish this?

    Thanks!

  18. Hello, You are seriously awesome for providing this tutorial. It’s really helped me get in the right direction. One problem though, could you give an example of how to redirect users based on city/region using the GeoLiteCity.dat. Basically I want to redirect everyone on the east coast to an east coast version of the site, and redirect west coast visitors to a west coast site. If I can accomplish that I will scream at the top of my lungs with happiness!! Thanks again Dinke!

  19. Hello Jason,

    For checking east vs. west cost you can either check based by zipcode (postcal code) and comparing it with some huge zipcode db but probably it’s much easier to set them up depending of Latitude/Longitude.

  20. Hi,
    Thankyou for this tutorial, i have been looking for this forever 🙂 You are Fantastic!
    i would like to redirect by region name
    is this possible using the free databases from maxmind?

    if not, is it possible by city?, i have tried the following but it is not working because i am obviously just guessing the code 🙂
    although i would prefer redirect by region name…

    <?php
    /**
    * Case Study – GeoIP Redirection
    *
    * @version $Id$
    * @package geoip
    * @copyright © 2006 Lampix.net
    * @author Dragan Dinic
    */

    require_once(“geoipcity.inc”);

    $gi = geoip_open(“GeoLiteCity.dat”,GEOIP_STANDARD);

    $city = geoip_city_by_addr($gi, $_SERVER[‘REMOTE_ADDR’]);

    geoip_close($gi);

    if($city == ‘Sydney’)
    {
    header(“HTTP/1.1 301 Moved Permanently”);
    header(‘Location: http://www.example.net/1/‘);
    }
    else
    {
    header(“HTTP/1.1 301 Moved Permanently”);
    header(‘Location: http://www.example.net/2/‘);
    }
    ?>

  21. In your example $ip = “89.216.226.174”; shows
    Your country is: Serbia and Montenegro
    Country Code is: CS

    Now it shows:
    Your country is: Serbia
    Country Code is: RS

    If this happened in a production version, we’d have change things all over.

    BTW: what other option are available when it comes to country data?
    Eg: geoip_country_name_by_addr gives u the country name. Are there any more things like this?

Leave a Reply

Your email address will not be published. Required fields are marked *