Jump to content

IP geolocation and redirect user to their language


Marco Ro
 Share

Recommended Posts

Hi, I looked in to the forum if there was any post but I have not found, it seems strange ... anyway.

Is it possible, using pw, based on the user's country redirect the users to their language page?
I was looking at IP geolocation services but they all seem to pay. is not there a system that does not require subscriptions?
or even better, an internal system in pw?

Link to comment
Share on other sites

ipinfo.io works well and have nice IPA system. But over 1000 requests for day need a bit expensive plan like 500$/y. So, the economic issue could be not a problem, but would be great if there is a solution that doesn't include accounts with external platforms.

But anyway, if I want use ipinfo and test it, how I can redirect the user. I can make a hook inside my ready.php ?

I have to use addHookBefore('Session::redirect', function(HookEvent $event)  ?

Or something like this, that at moment doesn't work...

<?PHP
function getUserIP(){
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
  
    if(filter_var($client, FILTER_VALIDATE_IP)){
        $ip = $client;
    } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
        $ip = $forward;
    } else {
        $ip = $remote;
    }
    return $ip;
}

$user_ip = getUserIP();

$http = new WireHttp();
$url = "http://ipinfo.io/{$user_ip}/country";
$response = $http->get($url, ['country' => '']);
$country = filter_var ( $response, FILTER_SANITIZE_ENCODED); // the country have a hiddend code

if ($country == "IT"){
    $session->redirect('/it');  
} elseif ($country == "FR") {
    $session->redirect('/fr');  
} else {
    $session->redirect('/');
} 

?>

 

Anyway I have to redirect always on the current page not always on the homepage. An also other problem it's that some people could choose to use the website form other languages. It's a bit tricky 😞  

 

Link to comment
Share on other sites

For me it's the worst you can do, and I hate it when I get redirected to a language I don't want. Sometimes you can't even get to another language. Just don't ever do automatic redirection based on anything.

  • Like 5
Link to comment
Share on other sites

@Soma yes, I understand you, but unfortunately in this case we are obliged because some pages have terms that should be used in the current language. Example, unfortunately not all know the VAT number equivalent that here in Italy is Partita IVA. These things and others can confuse users. 😞 

I would like in any case to leave the possibility to change the language for those who want, but the first language should be the one come form the country IP.

Link to comment
Share on other sites

you could set a users default language on registration / creation of the user by location of the ip address. if the user is not happy with that he can change it anytime. he will then see his favourite language on every login and you will save a lot of unnecessary ip/location lookups.

Link to comment
Share on other sites

yes @bernhard,  I will do this for this particular situation, because it's a subdomain that you must be logged for see the page and I will have the country code (we have decided now for the same reason you say you). But anyway In our next step we will open the e-commerce in the USA, we will make an other subdomain and we need to redirect all the USA traffic to this subdomain. so, if we do not find another solution we will use a service like ipinfo.io.

@Soma I know it's not the best but in this case we have to do in this way, special for the user came form the USA that will see different products compared to European ones. They will not have access to the UE site.

Link to comment
Share on other sites

19 hours ago, MarcoPLY said:

I was looking at IP geolocation services but they all seem to pay. is not there a system that does not require subscriptions?
or even better, an internal system in pw?

I make use of three different free services (in a fallback type scenario) in the Cookie Management Banner module:

https://github.com/adrianbj/CookieManagementBanner/blob/945d406d227c821a27be972f3a63ce4c4d44c613/CookieManagementBanner.module#L62-L85

ip.nf
geoip.nekudo.com
ip.sb

You can read a little about my conversation with the ip.nf folks here: 

 

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Imo language and country are different things.

In Switzerland we have four official languages. 

Use the Browser's HTTP Accept-Language, if you want a language preset.

Geolocation may be the right thing for country specific products or terms of conditions.

  • Like 3
Link to comment
Share on other sites

wow. Thank you very much @adrian the ip service look very nice! I will study the cookie modulo and also probably will implement it on the sites. Thanks again for sharing your knowledge!

Yes @theo, I have just find this post. I think will use the HTTP Accept-Language, it's a better way. For the country specific products I will use the geolocation service that suggested Adrian. 

I will Upload this post when I finish all the code. So, if some one will need in the future. 🙂

  • Like 2
Link to comment
Share on other sites

Ok, now redirect users based on their country works. So, for be honest I don't have do a lot, I use on of the free service @adrian write above and I have read this great post from @rayn (thank you). Anyway this code works quite well, but for sure it's possible write it better. 

<?php
function getUserIP() {
	$client  = @$_SERVER['HTTP_CLIENT_IP'];
	$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
	$remote  = $_SERVER['REMOTE_ADDR'];

	if(filter_var($client, FILTER_VALIDATE_IP)) {
			$ip = $client;
	}
	elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
			$ip = $forward;
	}
	else {
		$ip = $remote;
	}

	return $ip;
}

$user_ip = getUserIP();

$http = new WireHttp();
$url = $http->getJSON('https://geoip.nekudo.com/api/'.$user_ip);
$country = $url['country']['code'];

?>
<?php

foreach($languages as $language) {
  if(strpos($config->httpHost, "$language->yourwebsite.com") === 0) {
    $user->language = $language;
    break;
  }
}
$name = $sanitizer->pageName(substr($country, 0, 2));
$language = $languages->get($name);
if($language->id && $name != $user->language->name) {
	$url = "http://yourwebsite.com/" . $page->localUrl($language);
  $session->redirect($url);
}

 ?>

If the language it's not set for the country it will load the default language.  This can be used also for redirect users to any subdomain or whatever, just need change the $session->redirect($url).  substr it can be useful sometime this service give me back an hidden character.

I have just a notice in TracyDebug where start the foreach, if I put it after $country give me an error, look like there is an error in the previous lines but honestlyI did not understand what it is.

---

* In the Ryan post there is the code for filter the language by Browser's HTTP Accept-Language. As @theo had said.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...