PHP 会话变量随文件包含而变化 [英] PHP session variables change with file include

查看:42
本文介绍了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)

我在开发环境 + Apache 2 上使用 PHP 5.3.3 进行开发(我的代码在那里工作)生产服务器有 PHP 5.2.6 和相同的服务器(相同的代码在这里不起作用)

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)

感谢 Melsi 在另一个问题上,我设法将问题缩小到几行代码.

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.

结构是这样的:

-index.php
INCLUDE
    -menus.php
    -lang_fr.php
    -lang_en.php

文件如下:

索引.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>

菜单.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>&nbsp;&nbsp;
    <a href="/index.php?lang=fr">French</a>
EOT;

?>

LAN_EN 和 FR.PHP

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:

-首次加载:Array ( [lang] => fr )(默认,正确)

-First load: Array ( [lang] => fr ) (default, correct)

-点击英文链接:Array ( [lang] => Tn )

-点击法语链接:Array ( [lang] => Tr )

如果我将语言文件'Test'改为'Pest',上面的结果是'Pn'和'Pr'

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.

注意:当我删除 menus.php 中的包含时,问题就消失了

Note: The problem disappears when I remove the includes in menus.php

推荐答案

如果你仔细看我的 answear在您之前的问题中提到的第一件事(以粗体表示)正是这样的:

If you look closely to my answearin your previous question the very first thing mentioned (written in bold) was exactly this:

也许会话是从包含的文件开始的,这不应该发生!

Vineet 是正确的,我会扩大他的权利 answear 一点!

Vineet is correct and I will expand his right answear a bit more!

当您将文件 child.php 包含到father.php 中时,您必须将child.php 中的代码视为father.php 的一部分..php) 是会话开始.您不要在包含的脚本中启动会话,因为这可能会产生一些冲突,因为其他会话可能已经启动.

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?!

你说的是:

谢谢,但问题不是来自我网站的结构

Thanks but the problem doesn't come from the structure of my site

嗯,这可能不完全正确!问题是编写老式代码(没有 mvc,没有单一入口点,不是真正的面向对象)有一个非常容易学习曲线的好处.然而虽然这样的代码很容易写,但是这样的代码需要更多的技巧来避免错误!

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!

这篇关于PHP 会话变量随文件包含而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆