Posted by chlobe on January 28, 2009 at 11:45am
Jump to:
| Project: | IP to Country |
| Version: | 6.x-1.0-beta1 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
Hi, sorry for the lame question but I was hoping for some clarification of this module and its ability to serve as a page redirect according to user's IP address? I am having trouble configuring the module and would like to enquire as to possibility for paid assistance in setting up?
Thanks in advance
Comments
#1
Basic functionality of this module is APIs to convert ip to country. If you want to redirect according to IP, you need to write a short php snippet.
#2
Hi sugree, thanks for the prompt response - I did see a snippet which I thought was on your demo site http://www.howforge.com/product/ip2cc/demo but after visiting just now I can't seem to see it. I am not proficient in php so I am wondering if you could give me an idea of what it might look like? If you do paid consulting on the module I'd be more than happy to provide payment for assistance with teh configuration.
Cheers
#3
I do it in a custom module like this:
<?php
/**
* Implementation of hook_init().
*/
function mymodule_init() {
global $language;
global $base_url;
if ($_SERVER['SCRIPT_NAME'] != $GLOBALS['base_path'] .'index.php') return FALSE;
/**
* If the site is in offline mode there is little point doing any of this as you might end up redirecting to a 503.
*/
if (variable_get('site_offline', 0) == 1) return FALSE;
/**
* We only want to act if the user comes to the page from an external site/direct hit
*/
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) return FALSE;
/**
* If ip2cc code is not installed then we bail
*/
if (!module_exists('ip2cc')) return FALSE;
// only run this for the homepage
if (drupal_is_front_page()) {
// Get the Query String (minus the 'q'). If none set, set to NULL
$query_string = drupal_query_string_encode($_GET, array('q'));
if (empty($query_string)) {
$query_string = NULL;
}
// Establish the language prefix that should be used, ie. the one that drupal_goto() would use
$options = array('prefix' => '');
if (function_exists('language_url_rewrite')) {
// Note that language_url_rewrite() takes path (by reference) as the first argument but does not use it at all
$path = $_REQUEST['q'];
language_url_rewrite($path, $options);
}
$prefix = rtrim($options['prefix'], '/');
// Establish the users country
$ip2cc = ip2cc_get_country(ip2cc_get_user_ip_address());
$cc = strtolower($ip2cc->country_code);
// redirect uk users to the UK site
if ($cc == 'gb') $cc = 'en';
if (!$cc) return FALSE;
// redirect the user to a language specific version if
// 1. the country code maps to an enabled language
// 2. the country code is not the active language
// 3. the user has not asked for a specivic version
$languages = language_list('enabled');
foreach ($languages[1] as $lang) {
if ($lang->language == $cc && $language->language != $cc && !$prefix) {
drupal_goto($base_url .'/'. $cc .'/node', $query_string, NULL, 303);
return;
}
}
}
}
?>
#4
Automatically closed -- issue fixed for 2 weeks with no activity.
#5
If your still looking for a solution to redirect users based on IP address, I have just developed the IP2Locale module:
http://drupal.org/project/ip2locale