PHP 会话变量随文件包含而变化
这个问题基于之前的我问过的问题,但编辑变得混乱,因为我没有确定问题可能来自哪里。 (如果这个问题需要关闭,请告知)
我在开发环境+ Apache 2上使用PHP 5.3.3进行开发(我的代码在那里工作) 生产服务器有 PHP 5.2.6 和相同的服务器(相同的代码在这里不起作用)
感谢 Melsi 在另一个问题上的帮助,我设法将问题范围缩小到几行代码。
问题是:在包含文件中,我启动一个会话并检查变量。 根据该会话变量,我包含一个语言文件。
结构如下:
-index.php
INCLUDE
-menus.php
-lang_fr.php
-lang_en.php
文件如下:
INDEX.PHP
<?php
//SET LANGUAGE
if (isset($_GET['lang']) && $_GET['lang'] == 'fr') {
$_SESSION['lang'] = 'fr';
}
else if (isset($_GET['lang']) && $_GET['lang'] == 'en') {
$_SESSION['lang'] = 'en';
}
else {
$_SESSION['lang'] = 'en';
}
include_once 'include/menus.php';
?>
<html>
<head>
<title>building...</title>
</head>
<body>
<?php
echo($links);
?>
<br><br>
print_r($_SESSION);
<br><br>
<?php
print_r($_SESSION);
?>
</body>
</html>
MENUS.PHP
<?php
session_start();
if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {
include_once('lang_en.php');
}
else if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'fr') {
include_once('lang_fr.php');
}
else {
$_SESSION['lang'] = 'fr';
include_once('lang_fr.php');
}
$links = <<<EOT
<a href="/index.php?lang=en">English</a>
<a href="/index.php?lang=fr">French</a>
EOT;
?>
LAN_EN 和 FR.PHP
<?php
$lang['test'] = "Test";
?>
这在我的本地服务器上工作并在我单击链接时显示正确的会话变量。
在生产服务器上,我得到:
-第一次加载:Array ( [lang] => fr )(默认,正确)
-单击英文链接:Array ( [lang] => fr ) Tn )
- 单击法语链接:Array ( [lang] => Tr )
如果我将语言文件 'Test' 更改为 'Pest',上面的结果是 ' Pn' 和'Pr'
我想知道代码或配置生产服务器是否有问题(根据他们的支持,没有任何问题),如果有,可能是什么问题。
注意:当我删除menus.php中的包含时,问题消失了
This question is based on a previous question I asked but is getting messy with edits as I was not sure where the problem could come from. (Please advise if this question needs to be closed)
I develop with PHP 5.3.3 on development environment + Apache 2 (my code works there)
The production server has PHP 5.2.6 and the same server (same code doesn't work here)
Thanks to Melsi on the other question I managed to narrow down the problem to a few lines of code.
The problem is: In an include file I start a session and check for a variable.
Depending on that session variable I include a language file.
The structure is like this:
-index.php
INCLUDE
-menus.php
-lang_fr.php
-lang_en.php
The files are as follows:
INDEX.PHP
<?php
//SET LANGUAGE
if (isset($_GET['lang']) && $_GET['lang'] == 'fr') {
$_SESSION['lang'] = 'fr';
}
else if (isset($_GET['lang']) && $_GET['lang'] == 'en') {
$_SESSION['lang'] = 'en';
}
else {
$_SESSION['lang'] = 'en';
}
include_once 'include/menus.php';
?>
<html>
<head>
<title>building...</title>
</head>
<body>
<?php
echo($links);
?>
<br><br>
print_r($_SESSION);
<br><br>
<?php
print_r($_SESSION);
?>
</body>
</html>
MENUS.PHP
<?php
session_start();
if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {
include_once('lang_en.php');
}
else if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'fr') {
include_once('lang_fr.php');
}
else {
$_SESSION['lang'] = 'fr';
include_once('lang_fr.php');
}
$links = <<<EOT
<a href="/index.php?lang=en">English</a>
<a href="/index.php?lang=fr">French</a>
EOT;
?>
LAN_EN and FR.PHP
<?php
$lang['test'] = "Test";
?>
This on my local server works and displays the correct session variables when I click on the links.
On the production server I get:
-First load: Array ( [lang] => fr ) (default, correct)
-Click on English link: Array ( [lang] => Tn )
-Click on the French link: Array ( [lang] => Tr )
If I change in the language file 'Test' to 'Pest', the results above are 'Pn' and 'Pr'
I would like to know if there's something wrong with the code or with the configuration production server (according to their support there is nothing wrong) and if so what could be the problem.
Note: The problem disappears when I remove the includes in menus.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码中的问题是您在 Index.php 中设置设置变量并在 Menu.php 文件中启动会话。请将其更改为:
Index.php
MENUS.PHP
我认为这可以解决您的问题
The problem in your code is that you as setting the Setting variables and in Index.php but Starting the Session in Menu.php file. Kindly change thing to:
Index.php
MENUS.PHP
I Think this would resolve your problem
如果您仔细查看我的answear在上一个问题中提到的第一件事(以粗体写出)正是这样:
也许会话是从包含的文件开始的,这不应该发生!
Vineet 是正确的,我将扩展他的正确答案再多一点!
当您将文件 child.php 包含到father.php 中时,您必须将 child.php 中找到的代码视为father.php 的一部分您在father.php 脚本(如index.php)中要做的第一件事之一是一个会话开始。您不要在包含的脚本中启动会话,因为这可能会产生一些冲突,因为其他会话可能已经启动。
如果您有很多文件(如果由于没有单个入口点而直接包含或直接执行其中一些文件,则情况更糟)那么管理所有这些文件有多容易?!
你这样说:
,这可能不完全正确!问题是,编写老式代码(没有 MVC、没有单一入口点、不是真正面向对象)的好处是学习曲线非常容易。 然而虽然这样的代码很容易编写,但这样的代码需要更多的技能来避免错误!
另一方面,面向对象的方法更难开始,因为有更多的东西需要学习(对象、原型、接口、关系(属于、属于)等)并且需要不同的行为。 但是您肯定会受益更多!
最后一件事!好吧,一个结构良好的站点使会话管理只需几行,在开始时只写一次,仅此而已。
我很高兴你能解决你的问题!
If you look closely to my answearin your previous question the very first thing mentioned (written in bold) was exactly this:
Maybe a session is started from a file that is included and this should not happen!
Vineet is correct and I will expand his right answear a bit more!
When you include the file child.php into the father.php you must think of the code found in child.php as being part of father.php One of the first things you do in a father.php script (like index.php) is a session start. You do not start a session in an included script because this might create some conflict as an other session could have been started already.
And if you have many files, (even worse if some of them are both included or executed directly cause of no single entry point) then how easy is to manage all this?!
You said this:
Well this might not be entirely true! The thing is that writing old school code (no mvc, no single entry point, not really object oriented) has the benefit that has a very easy learning curve. HOWEVER while such code is easy to write the thing is that such code requires more skills to avoid errors!
On the other hand the object oriented aproach has more difficulty to get started cause there are more things to learn (objects, prototypes, interface, relatinships (belong-to, is part of) etc etc ) and requires a different behaviour. HOWEVER you definetely will benefit more!
A last thing! Well a well structred-site makes the session manage a thing of a few lines, writen only once at the very begining and that's it all.
I am glad that you are twoards solving you problem!