多语言站点适用于localhost,但不适用于Internet

时间:2011-02-13 17:44:22

标签: php html web multilingual

我创建了一个网站,其上有3个,并且网站工作正常(localhost)但是当我上传它时,默认lang是EN,每当我在网上更改lang时,让我们说从EN到FR,页面继续在这种情况下在FR上传播并在主页上重定向我,并且网站上的所有文本都在FR上,这没关系,但在那之后,当我尝试点击让我们说关于我们,然后在它自己更改它回到默认lang ,并告诉我那个页面(关于我们),但默认lang ... 以下是代码样本的样子......

这是select.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';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

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

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

default:
  $lang_file = 'lang.en.php';

}

include_once 'lang/'.$lang_file;

这是lang.en.php ...

$lang = array();

// menu
$lang['txt1'] = 'Home';
$lang['txt2'] = 'About Us'; 
$lang['txt3'] = 'Services'; 
$lang['txt4'] = 'Contact';

其他两种语言也一样......

我只是将index.php放入...

<ul>
    <li><a  href="index.php"><em><b><?php echo $lang['txt1'];?></b></em></a></li>
    <li><a  href="about.php"><em><b><?php echo $lang['txt2'];?></b></em></a></li>
    <li><a  href="services.php"><em><b><?php echo $lang['txt3'];?></b></em></a></li>
    <li><a  href="contact.php"><em><b><?php echo $lang['txt4'];?></b></em></a></li>
</ul>

当处于默认语言时,我可以正常导航,我可以在任何页面上没有问题,但问题是当我尝试将网站更改为其他语言时, 让我说我在www.example.com/about.php页面上,我想看看这个页面让我们说FR,网站会重定向我,在www.example.com/index.php?lang=fr(这是好的)然后我可以在网站上看到FR lang但只在主页上,但是当我尝试继续www.example.com/about.php然后网站放回默认的lang,在这种情况下EN ...

奇怪的是这个脚本在localhost上运行正常...
知道为什么吗?

更新

OUTPUT:
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )

<?php

session_start();
$lang = "lang value to put in session";
$something = "something value to put in session";
$_SESSION['lang'] = $lang;
$_SESSION['something_1'] = $something;
print_r($_SESSION);
$lang = "new lang value but not set in session";
$something = "new something value but not set in session";
print_r($_SESSION);

PHP不应该更改第二个数组

Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )

它应该是这样的:

Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => lang value to put in session [something_1] => something value to put in session ) 

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

  • 在设置会话之前,请确保您的文件没有回显任何内容
  • 如果您将文件保存为UTF,请确保它是没有BOM的UTF
  • 禁用display_errorsin ini文件。试试ini_set('display_errors',false);

我希望这有帮助,我多次遇到这个问题。