1

我想在每一页的顶部插入以下代码有问题。主要目标是更改语言设置。问题是当我发布表单时,它不会像它应该做的那样改变会话。

<?php

session_start();

$basename = basename($_SERVER['SCRIPT_NAME'], ".php"); 
$host = $_SERVER['HTTP_HOST'];
$index_path = $host.'/'.$basename;


if (isset($index_path)){
    //header ('Location:http://www.domain.com/'.$pref_language.'/'.$basename);
}
if (!isset($_SESSION['pref_lang'])){ //looks if this session already exists

    if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){//looks if the browser has set a default language
        $max   = 0.0;
        $languages = explode(",", (strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])));
        foreach($languages as $language){
            $language = explode(';', $language);
            $q    = (isset($language[1])) ? ((float) $language[1]) : 1.0;
            if ($q > $max){
                $max = $q;
                $pref_language = $language[0];
            }
        }
        $pref_language = trim($pref_language);
    }

    if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){// in case there is no http_accept_language create it by hand
        $pref_language = "de";  
    }
$_SESSION['pref_lang'] = $pref_language;  //registers the session in case there is no one
$pref_language = $_SESSION['pref_lang'];   //is needed to select the right footer

 }else{   //in case that there is already a session with the saved language

    if  (isset($_REQUEST["fr"])  ) {  //if a new language will be choosen overwrite the old session with the new entry and header to correct path to show the page in the desired language
        $_SESSION['pref_lang'] = 'fr';
        header ('Location: http://www.domain.com/'.$pref_language.'/'.$basename);
    }elseif (isset($_REQUEST["en"])  ) {
        $_SESSION['pref_lang'] = 'en';
        header ('Location: http://www.domain.com/'.$pref_language.'/'.$basename);
    }

    $pref_language = $_SESSION['pref_lang'];  //needed in case that the path is correct to select the right footer version
 }
var_dump($_POST);
var_dump($_SESSION);
echo '<form method="post">      
            <input type="submit" id="en" name="en" value="en"/><div>englisch</div>
        </form>
        <form method="post">  
            <input type="submit" id="fr" name="fr" value="fr"><div>französisch</div>
        </form>';
?>

什么时候

print_r ($_SESSION);

在另一个空白页面上,会话将是默认的“de”?

如果有人可以帮助我,我真的很感激。

多谢。

4

1 回答 1

1

试试这个版本,看看它是否对你有意义。请务必在第一次测试之前删除您的会话 cookie ;-)

<?php // RAY_temp_bonny.php
error_reporting(E_ALL);
session_start();

$form = <<<ENDFORM
<form method="post">
Choose Language:
<br/><input type="submit" name="pref_lang" value="de" /><div>german</div>
<br/><input type="submit" name="pref_lang" value="en" /><div>englisch</div>
<br/><input type="submit" name="pref_lang" value="fr" /><div>französisch</div>
</form>
ENDFORM;

// IF SOMETHING IS POSTED TO REQUEST THE LANGUAGE
if (!empty($_POST['pref_lang']))
{
    $_SESSION['pref_lang']  = $_POST['pref_lang'];
    $_SESSION['pref_basis'] = 'POST REQUEST VARS';
}
// IF NOTHING IS POSTED TO REQUEST THE LANGUAGE
else
{
    // IF THE SESSION PREFERENCE IS NOT ALREADY SET
    if (empty($_SESSION['pref_lang']))
    {
        // IF NO PREFERENCE IN THE REQUEST VARS, CHOOSE GERMAN
        if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
        {
            $_SESSION['pref_lang']  = "de";
            $_SESSION['pref_basis'] = 'DEFAULT CHOICE';
        }
        // IF THERE IS A CHOICE IN THE ACCEPT_LANGUAGE
        else
        {
            $_SESSION['pref_lang']  = strtolower(substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2));
            $_SESSION['pref_basis'] = 'HTTP_ACCEPT';
        }
    }
    // IF THE SESSION PREFERENCE WAS PREVIOUSLY SET
    else
    {
        $_SESSION['pref_basis'] = 'PREVIOUS SETTING';
    }
}

echo " CHOSEN LANGUAGE IS: " . $_SESSION['pref_lang'];
echo " AND THE REASON IS: "  . $_SESSION['pref_basis'];
echo $form;

如果没有发出特定的语言请求,这将使用默认值(“de”或 HTTP_ACCEPT)。如果客户端请求了语言,它将在会话中保留请求的语言。

于 2012-12-22T16:03:47.450 回答