0

I am working on a multilingual Drupal 7 site. I've been asked to set up an automatic redirect based on the user's location. I've tried the geo_redirect module but it does not work with internal URLs because it creates a redirect loop.

My site uses paths to switch languages. For example the German homepage is example.com/de.

It seems like there are various modules that say they can do this but some are just dev versions, some require a subscription to a geo-location service, and some like to populate the Drupal database with 200MB of geoIP data (huhhh???).

Here is an example of what I need...

  1. User located in Germany goes to example.com
  2. Website detects that they are located in Germany
  3. User gets redirected to example.com/de (the German homepage)
  4. If user in Germany goes to any URL with /de in it then no redirect occurs

Just wondering if anyone has any experience with this and how they went about doing it.

Thanks!

  • Can you write an example of what you want to do? – mchar Sep 13 '16 at 14:52
  • Just added an example. – Dustin Sep 14 '16 at 14:52
  • @Dustin I updated my code, Use the new one – M a m a D Sep 21 '16 at 4:51
1
+50

I would use Smart IP module.

Smart IP identify visitor's geographical location (longitude/latitude), country, region, city and postal code based on the IP address of the user. These information will be stored in session variable ($_SESSION) with array key 'smart_ip', Drupal $user->data object with array key 'geoip_location' in Drupal 6 and 7 and user data service using the parameters 'smart_ip' as module and 'geoip_location' as name in Drupal 8 of the user but optionally it can be disabled (by role) at Smart IP settings page. Other modules can use the function smart_ip_get_location($ip_address) in Drupal 6 and 7 and \Drupal\smart_ip\SmartIp::query($ipAddress) in Drupal 8 that returns an array containing the visitor's ISO 3166 2-character country code, longitude, latitude, region (FIPS), city and postal code. It provides a feature for you to perform your own IP lookup and admin spoofing of an arbitrary IP for testing purposes.

You can get the user location in different ways using this module.

<?php
$smart_ip_session = smart_ip_session_get('smart_ip');
if ($smart_ip_session['location']['country_code'] == 'PH') {
  drupal_set_message(t('You are in the Philippines.'), 'status');
}
?>

Or

<?php
if ($_SESSION['smart_ip']['location']['country_code'] == 'PH') {
  drupal_set_message(t('You are in the Philippines.'), 'status');
}
?>

Now, in the Theme_preprocess_html function of your theme in the template.php file, use the above codes to check if the user is in a right place. Create template.php file in your theme and use this function:

function themeName_preprocess_html(&$variables){
{
      $code = $_SESSION['smart_ip']['location']['country_code'];
      $currentLocation = arg(1); //it returns the /de or any language
      if(strtolower($code) != strtolower($currentLocation))
           drupal_goto("www.example.com/".strtolower($code));

}

If the default provides the language code, it works well but if not, you need to handle that case. For example if the default language is English and the /en is not coming after example.com, then you need to check if arg(1) == 'en'. It is easy to do.


update

To get the user language use this code

global $language;
print $language->language;

So your code will be

function themeName_preprocess_html(&$variables){
{
      global $language;
      $code = $_SESSION['smart_ip']['location']['country_code'];
      $currentLocation = $language->language;; 
      if(strtolower($code) != strtolower($currentLocation))
           drupal_goto("www.example.com/".strtolower($code));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.