I know I asked a similar question a little while ago but I thought it through more so I'm wondering if anyone knows how you could make a block display only for specific IP address ranges. The idea is that I want to show certain blocks only for people from certain countries to be able to further customize my user experience to their interests.

Comments

vm’s picture

duplicate of: http://drupal.org/node/256960

usually better practice to flesh out the same thread then to create a new one.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

siliconvalley1’s picture

ok well I found this by searching google:

<?php
global $base_url;
if ($base_url == 'http://00.00.00.00'){
  return TRUE;
} else {
  return FALSE;
}
?> 

Does anyone know how I could make it display a block for IP addresses pertaining to a specific country ( a range of IP addresses) rather than just one specific one?

cpugeniusmv’s picture

You can grab the IP address of the current user with ip_address().

Here's a function someone wrote to check to see if an IP is in a CIDR block:

http://us.php.net/manual/en/ref.network.php#74656

You would just have to figure out what the range(s) of IPs are for a country in CIDR and check for those.

siliconvalley1’s picture

Thanks for the response, I also just found this, I learned a new term "geotargeting" so now I have a better idea what I'm searching for:

http://drupal.org/node/43183

siliconvalley1’s picture

Pretty cool, it was right under my nose I just didn't know to search for "geotargeting" but came across the term on some other site that wasn't about drupal.

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

q8doc’s picture

I need that for an IP address, not a country, is that possible?

abqaria’s picture

subscribing

DavidVII’s picture

How do you do this for a specific IP range? Did this ever get figured out?

ennorehling’s picture

If you're not opposed to adding more dependencies to your project, add symfony/http-foundation to your composer.json file, then you can use \Symfony\Component\HttpFoundation\IpUtils::checkIp($addr, $cidr);

anonymous07’s picture

Subscribed. This is good info. Thanks