I want to show/hide blocks only if certain country codes prove TRUE. The problem is this module only works when someone logs in, I;ve tried running all of the README.txt functions as an anonymous visitor and none of it is working for me. It would be very helpful if a version of this rand an ip lookup on designated pages so I can alter pricing information depending on the country.

Thanks for your time,
scott

Comments

espurnes’s picture

Version: 7.x-1.x-dev » 6.x-1.3

Hi seagle/scott,

did you figured out how to solve it? I also want to show/hide information deppending on the user conutry, also for the users who are not logged in.

Any option to get this functionality for anonymous users?
I'm running drupal 6.22 with ip2country 6.x-1.3

Thanks.

espurnes’s picture

floeschie’s picture

Version: 6.x-1.3 » 7.x-1.x-dev

Subscribing, any news on this one?

mortician’s picture

Try this code add to block:

$ip = $_SERVER['REMOTE_ADDR'];
$country_code = ip2country_get_country($ip);
echo $country_code;
seagle’s picture

I did find a solution for this, and am guessing that something very similar would work in D6.

1. First I installed the ip2country module - http://drupal.org/project/ip2country This just installs the needed ip address to country table and provides you with some very valuable functions to use for verification.

2. Then I setup a custom block and ran it on pages that needed country controlled content, here is the code:

<?php
 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
//echo ($ip);
  $country_code = ip2country_get_country($ip);
//echo ($country_code); 
?>

3. Then you can control block/view visibility by using something like the following:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$country_code = ip2country_get_country($ip);
echo $country_code;
$args[0] = $country_code;
return $args[0];
?>

or

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$country_code = ip2country_get_country($ip);
$args[0] = $country_code;
if ($country_code != "US") {
do this...
}
?>

4. Finally, for testing I use these free proxy servers to see if the appropriate country codes are working and if the correct content is shown/hidden - http://www.proxy4free.com/list/webproxy_country1.html

Hope that helps, I spent countless hours getting this one worked out...

emberhxc’s picture

hi guys!

i'm in the exact same situation as #1, i have a 2 blocks that i need to enable/disable according to the visitors location!

I tried using the solution in #5, but i get an error:

Notice: Undefined index: HTTP_X_FORWARDED_FOR em eval() (linha 2 de mysite\modules\php\php.module(80) : eval()'d code).

does anyone know how to make this work? i've been searching for days for a solution to make this possible, but to no avail :(

seagle’s picture

I'm still running the solution I mentioned in #5 using the following code. Remember, you need the ip2country module setup, then try the following code, uncomment the echo's to see what is returned:

<?php 
$ip = $_SERVER['REMOTE_ADDR'];
$country_code = ip2country_get_country($ip);
echo $country_code;
?>

I vaguely recall the "$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];" syntax no longer working with newer version of PHP and switched to what you see above. So under a block you could run the "Pages on which this PHP code returns TRUE (experts only)" setting - be careful this can break your site if written incorrectly - and then run an if statement like the following:

if ($country_code == "US"){
  return TRUE;
} else {
  return FALSE;
}
chris_car’s picture

But what about the page cache for anonyous users? I am not sure the page is still served from the cache...

seagle’s picture

This definitely breaks if you're page-caching. I've gotten around that by installing Ajax Blocks (http://drupal.org/project/ajaxblocks) and putting the geoip code in an Ajax loaded block.

johncs’s picture

I wanted to hide my registration form. If you're in Australia, you can register. Outside Oz, you can view the site, you can login if you already have an account, but you cannot register.

The ip2country doesn't allow me to do this, but it has the data. I've created a little module, not really suitable for publication, that allows me to do this.

It implements hook_form_alter, and if you're not one of God's Chosen,

drupal_set_message(t('Please, This site is for chessplayers in Australia. You can view, but not join.'));
drupal_goto('/');

Note don't use the header to get the remote IP address, it's not that simple. instead, use $ip_address and, if you're using a proxy (I am), adjust settings.php.

I copied get_country() from ip2country.

Since implementing this, I have had only one registrant. Since this remains a test site, that is just fine.

tr’s picture

Status: Active » Closed (works as designed)

Everything I read above is consistent with what I expect.

This module provides an API, so you can use ip2country_get_country() in whatever context you like. To show a block/page depending on the visitor's country, for example. This usage is detailed in this thread above, but is also documented in the README.txt that comes with the module. Likewise, for the use case of only allowing a certain country to access a registration form - hook_form_alter() is the preferred way in Drupal to add this functionality to an arbitrary form.

The only thing this module does automatically is to determine a country for any authenticated (logged-in) user. But using ip2country_get_country() still works for anonymous users, as long as you're aware of the Drupal caching issues and issues that might be caused by other caching modules (e.g. Boost).

BarisW’s picture

Issue summary: View changes

I've just added a new module based on parts of the code above: https://drupal.org/project/ip_language_negotiation