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.