Changing Blocks based on Visitor's Country using GeoIP and Drupal


This site gets traffic from all over the globe, and sometimes I want to present different blocks to different users based on where they're located. For example, I might want to feature content about internationalization to visitors from Europe. In my case, visitors from Kenya sometimes find this site when they are looking for Davetech Group Kenya, a local computer retailer. My goal was to make a block appear on each page when the visitor is located in Kenya, directing them to the Davetech Group website. Here's how I did it:

Install the IP to Country Module

Download the latest release from the IP to Country module page at, extract it to your modules directory and enable it on your modules page at /admin/build/modules.

Load the IP Database

Visit your site's IP to Country configuration page at /admin/settings/ip2cc and click the "Update IP to Country" tab. The "Amount of entries" box should read zero. Under that you'll see inputs for "Start Entry" and "End Entry." The database file is large, and if you run through all 77,000 lines or so, your PHP process will probably time out (depending on your webhost.) My host was able to process about 50,000 lines per process, so I ran the update script once with values of 1 and 50,000, and again from 50,001 to 77,000.

Display the Country Code in Your Block

Either create a new block at /admin/build/block or choose an existing one. Choose an input format with the PHP filter enabled. Insert the following code into the block body to print the user's country code:


Use Visitor's Country to Determine Block Visibility

By the same token, you can leave the content of your block alone, but choose to show it depending on the country of your visitor. On your block page, scroll down to "Page-specific visibility settings" and choose the option "Show if the following PHP code returns TRUE (PHP-mode, experts only)." Now you can run a simple comparison, for example

&lt?php $co->country_code == "U" ?>

will show the block for only US visitors.



Totally great. Just what I was looking for. Question: should "Flush?" be checked when loading the IP database? What is "Flush?" for?

Dave's picture

Flush deletes all the data!

Looking at the module code, we see:

  if ($form_state['values']['flush']) {
    db_query("DELETE FROM {ip2cc}");

If you check "Flush" you will delete all data currently in your ip2cc table, so don't check it when you're first loading the data.

Hi ... thanks for the post ..

Hi ... thanks for the post ... just what I needed.

I tweaked the code for block visibility a little and now have ...

if ($co->country_code=="GB") {return TRUE;}
if ($co->country_code=="IE") {return TRUE;}

To show the block if it's UK or Ireland.

How to do this in drupal 7.

How to do this in drupal 7. the module is only for drupal 6 and 5.

Great one I think you saved

Great one I think you saved me life...also the idea from Kang Usep, was exactly what I was searching for. I have a shop which is running only in one little island country. No need to do oversee sales and for the people who are coming here on holiday a listing of products should be enough to keep them aware of the products. So want to reroute everyone not from this country to for example a static html page.

I tried the above method, the

I tried the above method, the block is getting disabled based on the visitors country ID. Instead the block should be visible is the country ID is true.

I tried it on D6 site.. for 'IN' (India) country code, unfortunately it does not work for me.

Did you enjoy this post? Please spread the word.