PHP4 在会话期间包含文件

发布于 2024-08-28 21:24:03 字数 943 浏览 4 评论 0原文

我正在尝试在我的网页上添加第二语言。我决定对不同的语言使用不同的文件,通过路径区分 - language/pl/projects.ln 包含波兰语文本,language/en/projects.ln - 英语。这些扩展只是为了区分其他语言文件,内容很简单 php:

$lang["desc"]["fabrics"]["title"] = "MATERIAŁY";
$lang["desc"]["fabrics"]["short_text"] = "Jakiś tam tekst na temat materiałów";
$lang["desc"]["services"]["title"] = "USŁUGI";
$lang["desc"]["services"]["short_text"] = "Jakiś tam tekst na temat usłóg";

然后在索引页面上我像这样使用它:

session_start();  
if (isset($_SESSION["lang"])) {  
    $language = $_SESSION["lang"];  
} else {  
    $language = "pl";  
}  
include_once("language/$language/projects.ln");
print $lang["desc"]["fabrics"]["title"];

问题是,如果未设置会话变量,则一切正常并且显示数组项内容,但是一旦我更改并设置 $_SESSION["lang"] 就不会显示任何内容。我通过将 print "sth"; 放在projects.ln 文件的开头来测试包含本身是否正常工作,并且与 $_SESSION["lang"]< 一起工作都正常/code> 设置和取消设置。

请帮忙。

I am trying to put second language on my webpage. I decided to use different files for different languages told apart by path - language/pl/projects.ln contains Polish text, language/en/projects.ln - English. Those extensions are just to tell language files from other, the content is simple php:

$lang["desc"]["fabrics"]["title"] = "MATERIAŁY";
$lang["desc"]["fabrics"]["short_text"] = "Jakiś tam tekst na temat materiałów";
$lang["desc"]["services"]["title"] = "USŁUGI";
$lang["desc"]["services"]["short_text"] = "Jakiś tam tekst na temat usłóg";

And then on the index page I use it like so:

session_start();  
if (isset($_SESSION["lang"])) {  
    $language = $_SESSION["lang"];  
} else {  
    $language = "pl";  
}  
include_once("language/$language/projects.ln");
print $lang["desc"]["fabrics"]["title"];

The problem is that if the session variable is not set everything works fine and array item content is displayed but once I change and set $_SESSION["lang"] nothing is displayed. I tested if the include itself works as it should by putting print "sth"; at the beginning of projects.ln file and that works all right both with $_SESSION["lang"] set and unset.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-09-04 21:24:03
  1. 您可以测试 session_start() 的返回值吗?如果为 false,则无法启动会话。
  2. 在向浏览器输出任何内容之前是否会调用它?如果标头已发送并且您的 error_reporting 级别太低,您甚至不会看到错误消息。
  3. 愚蠢,但是 - 您是否将 $_SESSION['lang'] 的值设置为“en”等有效值?当您在 else 块中使用它作为默认值而不是“pl”时,英语翻译是否正确加载?
  4. “Jakiś tam tekst na temat usłóg”-> “usług”:)

你能告诉我们这个输出是什么吗:

if(session_start()) {
    echo SID, '<br/>';
    if(isset($_SESSION['lang'])) {
        echo 'lang = "',$_SESSION['lang'], '"';
    }
}
  1. Can you test the return value of session_start() - if it's false, it failed to start the session.
  2. Is it being called before you output anything to the browser? If headers were already sent and your error_reporting level is too low, you won't even see the error message.
  3. Stupid, but - do you set value of $_SESSION['lang'] to valid value like "en"? Does the English translation load correctly when you use it as default value in else block instead of "pl"?
  4. "Jakiś tam tekst na temat usłóg" -> "usług" :)

Can you tell us what does this one output:

if(session_start()) {
    echo SID, '<br/>';
    if(isset($_SESSION['lang'])) {
        echo 'lang = "',$_SESSION['lang'], '"';
    }
}
赢得她心 2024-09-04 21:24:03

会话开始正常,我意外地修复了它。

我将 $_SESSION['lang'] 重命名为 $_SESSION['curr_lang'],现在可以正常工作了。看起来它不喜欢具有相同名称的数组和会话变量(?)。

Session starts fine and accidentally I managed to fix it.

I renamed $_SESSION['lang'] to $_SESSION['curr_lang'] and it now works allright. It seams like it didn't like the array and session variable having the same name (?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文