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.

  // 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.

Comments

Irrow’s picture

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

$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/

Sleeper_Service’s picture

This massively helped me out!

DavidVII’s picture

Is there a way for me to use this for an IP range? Say I want people in LA and NYC to see a particular block and everyone else doesn't. Can I use this? If so, any suggestions on how I would go about it? I'm relatively new to drupal and appreciate any help :)

DavidVII’s picture

I edited this so that you can display content based on an IP range. The script will check the first 12 characters and if it matches the user's IP it will display content 'A'. If not, it will display content 'B'. I used string compare to achieve this.

 	$ip = $_SERVER['REMOTE_ADDR'];

	// display according geotarget range
  	if (strncmp("xxx.xxx.xxx.", $ip, 11) == "0") {
    	$block = module_invoke('block', 'block', 'view', 95);
    	print $block['content'];
	} else {
		$block = module_invoke('block', 'block', 'view', 8);
		print $block['content'];
	}