我想在每個頁面的頂部插入以下代碼。主要目標是改變語言設置。問題是,當我發佈表單時,它不會改變會話,因爲它應該這樣做。會話不變?
<?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);
的其他空白頁上,該會議將是默認的 「德」?
如果有人能夠幫助我,我真的很感激。
非常感謝。
請參閱這裏的文章,教導這種事情的設計模式。我會盡力向你展示一個代碼示例。 http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_8910-A-Polyglot-Web-Site-in-PHP.html –