Page redirection according to access country

Asked

Viewed 1,390 times

1

I have 2 PHP pages, one in Portuguese and the other in English, and I would like the user to be automatically redirected to English if the access was outside Brazil. Is there any way ? Or the most effective solution is to create a link to the page in English, as it has on many websites.

  • http://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip

  • You can redirect the page if the user’s IP does not start with 200... is the first Brazilian IP number.

5 answers

1

You can use the $_SERVER["HTTP_ACCEPT_LANGUAGE"] php. It brings information sent from the browser.

<?php
 echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 // pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,gl;q=0.2

$arralang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = $arrlang[0];

switch ($lang) {
    case 'pt-BR':
      // redireciona para página em português
      break;
    case 'en-US':
    case 'en':
      // redireciona para página em inglês
      break;
    default:
      // redireciona para página em português
 }
  • there is a problem with that, I am in portugal, however after testing gave me "en-us"

  • Interesting Miguel!!! I did a quick search. It seems that Firefox gives preference to "en-US". Confirm which browser you used and, if possible, test in another browser for more data.

  • Yeah, that’s the same one, but I can’t test it on another one 'cause Chrome is crashing me right now

1

Depending on your PHP version, read the PHP documentation about Location class

1

You can use an API, in this case I used http://ip-api.com/, and make:

// descobrir o ip do utilizador
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

$ip_test = '97.162.48.46';
$local_json = @file_get_contents('http://ip-api.com/json/' .$ip_test); // aqui usa o $ip, este é só para teste

$countries_en = array('gb', 'us'); // definir codigo iso2 dos paises para os quais o site será apresentado em inglês
$lang = 'pt'; // linguagem default
if($local_json !== false) {
    $local_array = json_decode($local_json, true);
    if($local_array !== null && isset($local_array['countryCode'])) {
        if(in_array(strtolower($local_array['countryCode']), $countries_en)) {
            $lang = 'en'; // definir site com a linguagem Inglês
        }
    }
}
echo $lang; // en, pois o país (neste exemplo) são os EUA, cujo código é US, está dentro do nosso array $countries_en

Note that on the production server instead of $ip_test uses the $ip

0

You can also use this alternative by redirecting to default or language, if available.

echo $_SERVER['HTTP_ACCEPT_LANGUAGE']; // pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,gl;q=0.2     

$arrlang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = $arrlang[0];
$default_lang = "pt-br";

if(isset($lang)){
     header("Location: url/".$lang);
} else {
     header("Location: url/".$default_lang);
}

0

One way to do this is by using an API, which identifies the country, see the example below:

function isBrazil() {
    $location = file_get_contents('http://freegeoip.net/json/');
    if ($location) {
       $data = json_decode($location, true);
       if ($data['country_name'] == 'Brazil') {
          return true;
       }
    }
    return false;
}

if (isBrazil()) {
   header('Location: pagina-brasil.php');
} else {
   header('Location: pagina-ingles.php');
}

Or if you just want to redirect out...

 if (!isBrazil()) {
    header('Location: pagina-ingles.php');
 } 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.