Geotargeting block
Hi,
This is my first contribution to the wonderful Drupal.
If you are associated with Yahoo! Publisher Network (advertisement network), you probably already know, that they count only US traffics. Furthermore, if you have too much non-US traffic, they can decide to ban your account from their network. This block snippet provides the functionality of geographical targeting your visitors, and if someone is not from US, some other content is shown to them.
<?php
// make a valid request to the hostip.info API
if ($_SERVER['HTTP_X_FORWARD_FOR']) {
$ip = $_SERVER['HTTP_X_FORWARD_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$url = "http://api.hostip.info/country.php?ip=".$ip;
// fetch with curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$country = curl_exec($ch);
curl_close ($ch);
// display according geotarget
if ($country == "US") { // show Yahoo!
$block = module_invoke('block', 'block', 'view', 4);
print $block['content'];
} else { // show AdBrite
$block = module_invoke('block', 'block', 'view', 1);
print $block['content'];
}
?>This snippet uses the hostip.info API to determine the geographical location of the visitor. In the code above, I have 2 ad blocks (yahoo and adbrite). Both of them are disabled by default and only their content is being used.
Of course, you can show other things, not only advertisements :).
If you have some other idea for geotargeting, just share :). I'm looking for a way to not use remote services like hostip.info but some kind of local scripts instead, but for now this is the only _freeware_ way.

Alternatively you may want
Alternatively you may want to use this snippet, its what we use to only display a news block to people from Australia who are looking at the front page. It works with 5.x
<?php$co = ip2cc_get_country($_SERVER['REMOTE_ADDR']);
if ( $co->country_code2 == 'AU' && arg(0) == 'front_page') {
return true;
} else {
return false;
}
?>
You'll need to get the IP to Country module, which you can do by going here. The added advantage is you don't have to rely on using an external service that may be slow / unreliable.
-------
http://www.irrow.com/