0

我正在努力使用基于语言检测将用户重定向到其他页面的代码。我发现这段代码看起来很有希望,因为到目前为止我还没有从这个网站上的其他帖子中获得任何运气。我唯一的问题与第一行代码有关。我在第一行的“”部分放了什么?

<?php
$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
    if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
// Now we simply evaluate that variable to detect specific languages
if($lc == "fr"){
    header("location: index_french.php");
    exit();
} else if($lc == "de"){
    header("location: index_german.php");
    exit();
}
?>
<h2>Hello Default Language User</h2>
<h3><?php echo "Your 2-letter Code is: ".$lc; ?></h3>

当我运行此代码时,我收到一条错误消息:

警告:无法修改标头信息 - 第 12 行 /home/m3418630/public_html/sumoresources/index.php 中的标头已由(输出开始于 /home/m3418630/public_html/sumoresources/index.php:3)发送

谁能解释为什么会这样?

谢谢

4

3 回答 3

0

你没有放任何东西,它只是像评论中所说的那样启动变量。

如果您收到标头已发送错误,这意味着您在发送标头()之前将信息输出到页面,如果不使用ob_start()ob_end_flush()进行缓冲,您将无法执行此操作。

于 2013-08-08T07:44:19.517 回答
0

此代码段可以检测用户浏览器中的语言

    $locale = null;
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {   
            $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

            foreach ($languages as $lang) {

                $lang = str_replace('-', '_', trim($lang));

                if (false === strpos($lang, '_')) {

                        $locale = strtoupper($lang);
                } else {

                    $lang = explode('_', $lang);

                    if (count($lang) == 3) {
                        $locale = strtolower($lang[0]) . ucfirst($lang[1]) . strtoupper($lang[2]);
                    } else {
                        $locale = strtolower($lang[0]) . strtoupper($lang[1]);
                    }

                }
            }
        }
echo $locale;

http://codepad.viper-7.com/F1XfU5

于 2013-08-08T07:56:50.990 回答
0

在您的代码片段中,您必须为变量设置默认语言$lc。如果服务器从当前请求发送语言代码,它将被覆盖。

于 2013-08-08T07:49:08.590 回答