Change language of the site

Asked

Viewed 2,255 times

0

I have a problem with the site’s languages, it has 4 languages and when you click on the other languages there are times that it exchanges and most of the time it does not exchange, I do not know if the information is stored in the cache. I was not the one who developed the site so it gets a little more complicated to pass here, in case some information lacks tell me that I look for.

Here is the language mapping

<map name="MapIdioma">
            <area shape="rect" coords="0,2,19,19" href="index.php?idioma=portugues">
            <area shape="rect" coords="24,2,43,18" href="index.php?idioma=english">
            <area shape="rect" coords="46,2,66,19" href="index.php?idioma=espanol">
            <area shape="rect" coords="69,2,89,17" href="index.php?idioma=alemao">
</map>

And here are the sessions that begin

if ($_REQUEST['idioma'] == 'portugues')
{
    $_SESSION['idIdioma']   = '1';
    /*header ("Location: ".$_SERVER['HTTP_REFERER']);*/
    echo "<script>document.location='".$_SERVER['HTTP_REFERER']."'</script>";
    exit();
}

if ($_REQUEST['idioma'] == 'english')
{
    $_SESSION['idIdioma']   = '2';
    /*header ("Location: ".$_SERVER['HTTP_REFERER']);*/
    echo "<script>document.location='".$_SERVER['HTTP_REFERER']."'</script>";
    exit();
}

if ($_REQUEST['idioma'] == 'espanol')
{
    $_SESSION['idIdioma']   = '3';
    /*header ("Location: ".$_SERVER['HTTP_REFERER']);*/
    echo "<script>document.location='".$_SERVER['HTTP_REFERER']."'</script>";
    exit();
}

if ($_REQUEST['idioma'] == 'alemao')
{
    $_SESSION['idIdioma']   = '4';
    /*header ("Location: ".$_SERVER['HTTP_REFERER']);*/
    echo "<script>document.location='".$_SERVER['HTTP_REFERER']."'</script>";
    exit();
}

if (!session_is_registered('idIdioma'))
    $_SESSION['idIdioma'] = '1';

if ($_SESSION['idIdioma'] != '1' and $_SESSION['idIdioma'] != '2' and $_SESSION['idIdioma'] != '3' and $_SESSION['idIdioma'] != '4')
    $_SESSION['idIdioma'] = '1';

if ($_SESSION['idIdioma'] == '1')
    include 'idioma.portugues.php';

if ($_SESSION['idIdioma'] == '2')
    include 'idioma.ingles.php';

if ($_SESSION['idIdioma'] == '3')
    include 'idioma.espanhol.php';

if ($_SESSION['idIdioma'] == '4')
    include 'idioma.alemao.php';
?>
  • 1

    Off-line: english, espanol, alemao, portugues? Set a default for language names. Suggestion: english, espanol, deutsch, portugues.

  • Some reason why idIdiome is a string and not an integer? By the way, relying too much on HTTP_REFERER is problematic. http://stackoverflow.com/questions/5934747/is-serverhttp-referer-safe (link in English, the first two answers are of interest)

1 answer

2

I couldn’t identify the problem, it might be something outside the scope of the script you gave us, but I made it free to rewrite your script.

https://gist.github.com/juniorb2ss/a3db0d9b0fab03406a89

$arrayIdiomas = array('portugues' => 'idioma.portugues.php', 'english' =>    'idioma.ingles.php', 'espanol' => 'idioma.espanhol.php', 'alemao' => 'idioma.alemao.php');

// Definindo o idioma padrão
if(!isset($_SESSION['idIdioma'])){
   $_SESSION['idIdioma'] = 'portugues';
}


 if(isset($_REQUEST['idioma'])){
 // o idioma seleciona existe?
  if(array_key_exists($_REQUEST['idioma'], $arrayIdiomas)){
    $_SESSION['idIdioma'] = $_REQUEST['idioma']; // seta o idioma selecionado
    echo "<script>document.location=$_SERVER['HTTP_REFERER']</script>"; // refresh na página
    exit();
 }
}
else{
 // idioma definido na sessão é valido?
 if(array_key_exists($_SESSION['idIdioma'], $arrayIdiomas)){
    include $arrayIdiomas [$_SESSION['idIdioma']]; // inclue o arquivo.
 }
}

I did not test, but I believe that this working, in this form will be more readable your code.

  • In the document.location It stopped being a string. And maybe it would be better to do some sort of HTTP_REFERER check, in case it was intercepted (by a Man-in-the-Middle attack, for example)

  • Send to a predefined url.

Browser other questions tagged

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