PHP会话变量与局部变量互换? [英] PHP session variables interchanged with local variables?

查看:122
本文介绍了PHP会话变量与局部变量互换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于php中的会话变量和局部变量,我遇到了一个非常奇怪的问题.

I've encountered a very odd issue in regards to session variables and local variables in php.

我试图弄清楚我是否对php中的会话不了解,或者这是否是我的主机使用的php版本的问题.

I'm trying to figure out if I am not understanding something about sessions in php or if this is an issue with the php version my host is using.

这里有一个非常简单的代码来演示这个奇怪的问题:

Here is a very simple code to demonstrate the weird issue:

session_start();  
var_dump($kenny);  
var_dump($_SESSION['kenny']);  
$_SESSION['kenny']='def';  
var_dump($kenny);  
var_dump($_SESSION['kenny']);  
$kenny = 'abc';  
var_dump($kenny);  
var_dump($_SESSION['kenny']);  

我第一次运行代码时,得到以下结果(正如人们期望的那样):

The first time I run the code, I get the following results (as one would expect):

NULL NULL NULL string(3) "def" string(3) "abc" string(3) "def"

我第二次运行它(当然不关闭浏览器),现在我明白了!

I run it a second time (without closing my browser, of course), I get this now!

string(3) "def" string(3) "def" string(3) "def" string(3) "def" string(3) "abc" string(3) "abc" 

我第三次,第四次,第五次运行,依此类推,我明白了!

I run it a 3rd, 4th, 5th time and so on, I get this!!!

string(3) "abc" string(3) "abc" string(3) "def" string(3) "def" string(3) "abc" string(3) "abc" 

在我看来,在多次运行脚本之后,会话变量'kenny'和局部变量$ kenny成为另一个的别名.嗯...我真的不认为这是会话变量和局部变量在php中的工作方式.如果我在这里缺少任何东西,请纠正我.

It looks to me like the session variable 'kenny' and local variable $kenny become aliases to one and the other after running the script more than once. hmm... I really don't think this is how session variables and local variables work in php. Please correct me if I'm missing something here.

我的网络主机正在运行php 5.2.2.当我在运行php 5.2.1、5.2.14和5.3.1的其他主机上尝试使用完全相同的代码时,它们总是给我我期望的东西:

My web host is running php 5.2.2. When I try this exact same code on other hosts running php 5.2.1, 5.2.14 and 5.3.1, they always give me what I expect:

第一次:

NULL NULL NULL string(3) "def" string(3) "abc" string(3) "def"

此后:

NULL string(3) "def" NULL string(3) "def" string(3) "abc" string(3) "def" 

我检查了php.net上的更改日志,但没有找到与我相关的任何内容可以解决此问题.但是就像我提到的那样,早期版本(5.2.1)可以正常运行,所以这让我感到非常困惑.

I checked the change log on php.net and didn't find anything that I can relate to that may address this issue. But like I mentioned, an earlier build (5.2.1) works ok, so that's very puzzling to me.

如果有人运行任何其他版本的php 5.2.x,请尝试一下,如果遇到相同的问题,请通知我.或者,如果有人对此问题有任何见解,我将非常感谢您提供任何反馈意见.

If anyone runs any other version of php 5.2.x, please give it a try and let me know if you see the same issue. Or if anyone has any insight into the issue, I'd really appreciate any feedback.

感谢一百万!

推荐答案

这可能是因为

This is probably because the register_globals directive is on. It doesn't say it on that page that $_SESSION variables are included, but it says here:

如果启用了register_globals,则 全局变量和 $_SESSION条目将自动 引用相同的值 在上届会议上注册 实例.但是,如果变量是 由$_SESSION注册,然后 全局变量自 下一个请求.

If register_globals is enabled, then the global variables and the $_SESSION entries will automatically reference the same values which were registered in the prior session instance. However, if the variable is registered by $_SESSION then the global variable is available since the next request.

这篇关于PHP会话变量与局部变量互换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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