浏览器PHP重定向无法正常工作!

时间:2010-02-27 15:33:54

标签: php browser redirect

我使用php数组更改语言,这使得URL看起来像这样:

http://alexchen.zxq.net/index.php?lang=es

我在默认部分的'controller'文件中添加了$lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];,我在首选语言选项中选择了另一种语言(Mozilla Firefox), 但它不起作用。

的common.php:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
 $lang = $_GET['lang'];

 // register the session and set the cookie
 $_SESSION['lang'] = $lang;

 setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
 $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
 $lang = $_COOKIE['lang'];
}
else {
 //$lang = 'en'; <-this was previous code
        //I tried this:
        $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}

// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
 $lang_file = 'lang.en.php';
 break;

case 'es':
 $lang_file = 'lang.es.php';
 break;

case 'tw':
 $lang_file = 'lang.tw.php';
 break;

case 'cn':
 $lang_file = 'lang.cn.php';
 break;

default:
 //$lang_file = 'lang.en.php'; <-this was before
        //I also tried this:
        $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}

//translation helper function
function l($translation) {
 global $lang;
 return $lang[$translation];
}

 include_once 'languages/'.$lang_file;
?>

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

$_SERVER['HTTP_ACCEPT_LANGUAGE']比两个字母的ISO代码更复杂。查看this question,了解如何在PHP中解析HTTP_ACCEPT_LANGUAGE