How-to use maxMind GeoIP for websites

Sometimes you need to redirect visitors based on the country they come from. Maxmind provides a great country/IP address database in binary format. The following example will show you how redirect visitors from countries which are not on the “whitelist”.

Download the latest Geo data and the class file here.

Copy the class file and the GeoIP data file into the same directory where the page is located (most of the time the root directory)

Include the class file inside your page and add the following code to request the country using the following code (below the include statements):

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

Define some country codes as the “whitelist”:

$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');

Next place the following code below the $mycountries array:

if (!in_array(strtolower($country), $my_countries)) {
  header('Location: some URL...');
  exit;
}

Along with using maxMind GeoIP to redirect visitors based upon their country, you can use it to Hide Adsense from your site visitors based on their country

5 thoughts on “How-to use maxMind GeoIP for websites”

  1. Hey there!
    Thanks for the great info
    I’m having troubles while trying to set that up though.

    I don’t have any experience with PHP and I’m using BlueHost right now as my hosting service.

    What I did is this:
    I uploaded GeoIP.dat and geoip.inc to the folder where my index page is located.

    I added the file index.php
    Inside, I wrote this script:

    require_once('geoip.inc');
    
    $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    
    $my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
    if (!in_array(strtolower($country), $my_countries))
    {
    header('Location: http://www.google.com');
    exit;
    }
    ?>

    I uploaded it to my web server (same directory as geoip.inc + GeoIP.dat). But it does not work.
    What am I doing wrong?

    More specifically, what I need to do is redirect all traffic to an URL “X”, but the traffic that comes from certain countries “A”+”B”+”C” need to be redirected to another URL “Y”.

    Any help would be GREATLY appreciated. I’m struggling to make this work, and it is really important for me right now.
    Looking forward to hearing from you!

  2. What do you get if you place an “echo” before the variable $country?

  3. Ok, I’ve figured out the issues and made it work.

    My first mistake was not to put <?php at the beginning of the page.

    I couldn’t make it work with that array thingy. I tested it with proxies from different countries but was always redirected to the same page.

    I used this script and it is working like it should:

    <?php
    require_once("geoip.inc");
    
    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    
    $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    
    if($country_code == 'US') {
            header('Location: http://USTraffic.com');
    } elseif($country_code == 'DE') {
            header('Location: http://DETraffic.com');
    } elseif($country_code == 'GB') {
            header('Location: http://GBTraffic.com');
    } elseif($country_code == 'CA') {
            header('Location: http://CATraffic.com');
    } elseif($country_code == 'BR') {
            header('Location: http://BRTraffic.com');
    } elseif($country_code == 'TR') {
            header('Location: http://TRTraffic.com');
    } else {
            header('Location: http://AllTraffic.com');
    }
    ?>
  4. You need to add exit after each header call, check all the switch function instead of so much else/if statements.

  5. I use this snippet to switch ads based on the visitor’s country code:

    $gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
    $country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    
    $my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
    if (in_array($country, $my_countries)) {
    	$rectangle = '
    <script type="text/javascript"><!--
    google_ad_client = "pub-XXXXXXXXXX";
    google_ad_width = 250;
    google_ad_height = 250;
    google_ad_format = "250x250_as";
    google_ad_type = "text";
    google_color_border = "FFFFFF";
    google_color_bg = "FFFFFF";
    google_color_link = "0000FF";
    google_color_text = "333333";
    google_color_url = "87C32C";
    //--></script>
    <script type="text/javascript"
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>';
    } else {
    	$rectangle = '
    <!-- Begin BidVertiser code -->
    <SCRIPT LANGUAGE="JavaScript1.1" SRC="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=000&bid=000" type="text/javascript"></SCRIPT>
    <!-- End BidVertiser code -->';
    }
    </code>

Leave a Reply

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

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