stblink VIP
Total posts: 501
08 Feb 2013 15:31

Since Chameleon is not J3.0, i believe i have found a way to redirect users by ip to a specific language with a very small hack.

I know that with Metamod i can display modules by ip.

But how about content? In this case Cobalt.

For example:

If user comes from Brasil, i want to show by this order:

1 - featured records that have Brasil in the geomap country field

2 - the not-featured records from Brasil

3 - Other records from other countrys

I know this could be accomplished easier using a different section for each country, but i want the search to be applied to all countrys.

Last Modified: 02 Mar 2014


Sergey
Total posts: 13,748
09 Feb 2013 22:05

I would not use IP to set language. The best way is to set language of OS. For example I have Russian windows, and Russian language is installed.

You know I hate to see Philippino Google every time I access it although I am logged in and I set English as default language.

Better is to take locale with Javascript and then redirect.

Something like this

https://github.com/dansingerman/jQuery-Browser-Language

That is because the fact I open your site from India does not mean at all I speak Hindi. But if my PC locale is hindi, does not matter from what country I access your site.


stblink VIP
Total posts: 501
11 Feb 2013 08:34

It's something i will take a look later, thanks!

And is it possible to show the records according to what i mentioned?

thanks


Sergey
Total posts: 13,748
12 Feb 2013 02:38

That is complicated issue. Of course you can redirect URL to appropriate language either by IP or Locale. But what if it is wrong and user want to switch to the other language? You should not redirect him anymore. So there have to be mechanism to stop detecting language after explicit user language set.

I mean I cannot tell you in few words how to do that. But of course with custom code you can do anything :)


stblink VIP
Total posts: 501
12 Feb 2013 10:05

Well, the redirect script is this one:

if (isset($_COOKIE["geoip"])) {

$country_code=$_COOKIE['geoip'];

}

else {

include("geoip.inc");

$ip=$_SERVER['REMOTE_ADDR'];

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

// Country name is not used so commented

// Get Country Name based on source IP

//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

setcookie('geoip',$country_code,time() + (86400 * 7)); // 86400 = 1 day

switch($country_code)

{

    case "PT": header("Location:  http://mysite.com/index.php?lang=pt "); break;

    case "BR": header("Location:  http://mysite.com/index.php?lang=br "); break;

    default: header("Location:  http://mysite.com/ ");

}

}

So a cookie is set to determine to which language the user is "redirected". Although there is still the language switcher so that the user has the abillity to always change it.

If the cookie is set, means the user has already visited the site and no redirects will be made.. although the fact that expires in 1 day... does it mean that after 1 day it will redirect again ?

Theres another option to "play around" which is the Falang cookie:

if (isset($_COOKIE["geoip"])) {

$country_code=$_COOKIE['geoip'];

}

else {

include("geoip.inc");

$ip=$_SERVER['REMOTE_ADDR'];

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

// Country name is not used so commented

// Get Country Name based on source IP

//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

setcookie('geoip',$country_code,time() + (86400 * 7)); // 86400 = 1 day

switch($country_code)

{

    case "PT": header("Location:  http://mysite.com/index.php?lang=pt "); break;

    case "BR": header("Location:  http://mysite.com/index.php?lang=br "); break;

    default: header("Location:  http://mysite.com/ ");

}

}

Name 95c832d18647c5536be1c3a76ff3e664

Value pt-BR

Host localhost

Path /

Expires Wed, 12 Feb 2014 16:01:29 GMT

Secure No

HttpOnly No

The problem is that appears to be the name which i believe it's random. Not beeing random, i believe i could check if the Falang cookie is set and if it is... do not redirect anymore.

Powered by Cobalt