I have a multi-language Joomla 3.4 that at the moment only has 2 languages:
- my-domain.com/en/
- my-domain.com/es/
I want to redirect the visitor to EN/ES according to their IP (with this library).
I put my redirect code in the /templates/my-template/index.php file. This is my code:
if ( !isset($_SESSION["client_country"]) ) {
$_SESSION["client_country"] = getCountryFromIP($_SERVER["REMOTE_ADDR"]);
$client_country = $_SESSION["client_country"];
$_SESSION["total_redirections"] = 1;
if ( ($client_country == "EN") || ($client_country == "UK") ) {
header('Location: http://www.my-domain.com/en/');
}
else {
header('Location: http://www.my-domain.com/es/');
}
}
else {
$_SESSION["total_redirections"] ++;
$client_country = $_SESSION["client_country"];
if ( $_SESSION["total_redirections"] <= 2 ) {
if ( ($client_country == "EN") || ($client_country == "UK") ) {
header('Location: http://www.my-domain.com/en/');
}
else {
header('Location: http://www.my-domain.com/es/');
}
}
}
The problem:
- My first and second visits: I am correctly redirected to EN/ES according to my IP.
- My third and subsequent visits: I am no longer redirected to EN/ES
according to my IP (I'm redirected to the website's default
language).
How do I fix this?
Thank you!