views:

72

answers:

2

Hi, I have a drupal site, and i want to redirecting visitors to a different page based on the country. I have this code:

require_once "Net/GeoIP.php";
$geoip = Net_GeoIP::getInstance("Net/GeoIP.dat");
try {
    $geocode = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
} catch (Exception $e) {
    $geocode = 'EN';
}
switch ($geocode) {
    case 'HU':
        header('Location: http://www.example.com/hu');
        break;
    case 'GB':
        header('Location: http://www.example.com/en');
        break;
    case 'AT':
        header('Location: http://www.example.com/at');
        break;
    case 'CY':
        header('Location: http://www.example.com/cy');
        break;
    case 'DE':
        header('Location: http://www.example.com/de');
        break;
    case 'NL':
        header('Location: http://www.example.com/nl');
        break;
    case 'CH':
        header('Location: http://www.example.com/ch');
        break;
    case 'ES':
        header('Location: http://www.example.com/es');
        break;
    case 'US':
        header('Location: http://www.example.com/us');
        break;
    default:
        header('Location: http://www.example.com/en');
}

This works well on plain php file. How can i this in drupal? How can i redirect the visitors to the proper node?

A: 

You may want to use http://drupal.org/project/ip2locale

It supports GeoIP.

Sid NoParrots
@turbod: was this useful?
Sid NoParrots
Sorry No, I can redirect users without any module. I want to apply my code. This code works, but I've never used in drupal page.
turbod
Why do you want to apply your own code? The whole purpose of Drupal is to use pre-existing modules so you don't need to re-invent the wheel. The module I suggested will achieve the _same_ thing you want. Why don't you try out and see if it fits your needs? If it doesn't fit your needs then you can try using your custom code.
Sid NoParrots
A: 

I don't understand neither why not using an existing solution but still: have you tried putting your snippet inside page.tpl.php? Just make sure the path to the required library is correct. You might wanna put Net/GeoIP.php inside your sites/all/libraries folder and thus change require_once "Net/GeoIP.php" to require_once "../../libraries/Net/GeoIP.php". This is assuming that your page.tpl.php is located inside sites/all/themes/yourtheme/

klickreflex