You are hereBlogs / dave's blog / Changing Blocks based on Visitor's Country using GeoIP and Drupal

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


By dave - Posted on 30 January 2009

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 drupal.org, 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:

<?php
$co=module_invoke('ip2cc','get_country',$_SERVER['REMOTE_ADDR'];);
print($co->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

<?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?

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.

Any ideas how to do the same with US cities, so the sub-domain will reflect user location? Like losangeles.citysearch.com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Tags associated with this post

About dave

dave's picture

Name
Dave Nugent

Bio

Dave is a freelance Drupal developer working in the San Francisco bay area. He recently graduated from Carnegie Mellon University Silicon Valley. In his spare time he docents at the Computer History Museum. Follow Dave on Twitter @drnugent.