简体   繁体   English

PHP声明全局变量和会话变量

[英]PHP declaring global and session variables

I'm been using PHP5.3.8 for a while now and I've just come across a new error for me; 我已经使用PHP5.3.8一段时间了,我刚刚遇到了一个新错误。

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. 警告:未知:您的脚本可能依赖于PHP 4.2.3之前存在的会话副作用。 Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. 请注意,除非启用了register_globals,否则会话扩展不会将全局变量视为数据源。 You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. 您可以通过将session.bug_compat_42或session.bug_compat_warn分别设置为off来禁用此功能和此警告。 in Unknown on line 0 在第0行的Unknown中

To which there is a solution to here; 对此有解决方案; PHP session side-effect warning with global variables as a source of data . 将全局变量作为数据源的PHP会话副作用警告

However, I still don't understand why this is happening. 但是,我仍然不明白为什么会这样。 As far as I'm aware, there should be no ambiguity between $foo and $_SESSION['foo'] ? 据我所知, $foo$_SESSION['foo']之间应该没有歧义?

I've tried testing a few of the scripts online that people say throw this error, but they don't throw the warning on my system, and the script I'm getting this warning on is hella complicated and stripping it down to a few lines in order to replicate the error is proving problematic. 我已经尝试过在线测试一些有人说会抛出此错误的脚本,但是他们不会在我的系统上抛出警告,而我收到此警告的脚本非常复杂,将其简化为几个行以复制错误被证明是有问题的。

However, I have narrowed it down to one line; 但是,我将其范围缩小到了一行。

$bonus = isset($_GET['bonus']) ? $_GET['bonus'] : '';

$_SESSION['bonus'] is set to null in a previous page, and register_globals has been off since about PHP4.2 something, so can't be the $_GET scope. $_SESSION['bonus']在上一页中设置为null ,并且register_globals自PHP4.2以来一直处于关闭状态,因此不能成为$ _GET范围。

So this brings me back to my first point; 因此,这使我回到了第一点。 why is there suddenly an issue between $bonus and $_SESSION['bonus'] ? 为什么突然在$bonus$_SESSION['bonus']之间出现问题? I say suddenly, because I am using this exact script on another site (same box) and I get no errors. 我突然说,因为我在另一个站点(同一框)上使用了这个确切的脚本,并且没有任何错误。

Could someone point me to anywhere that perhaps explains in more detail how PHP deals with session vars etc and why there is an issue between the two variables? 有人可以指出任何可能更详细地解释PHP如何处理会话变量等的地方,以及为什么两个变量之间存在问题吗?

Thanks. 谢谢。

[ Update ] [ 更新 ]

Looking here; 看这里; PHP Runtime Configuration it does mention that: PHP Runtime Configuration确实提到:

[PHP has...] an undocumented feature/bug that allows you to initialize a session variable in the global scope... [PHP具有...]一个未公开的功能/错误,可让您在全局范围内初始化会话变量...

I guess there may not be much more to say other than that. 我想可能没有别的话要说了。 It just seems odd to me that PHP has issues with $_SESSION['foo'] / $foo when $_SESSION['foo'] != $foo ... 在我看来,当$_SESSION['foo'] != $foo ...时,PHP的$_SESSION['foo'] / $foo有问题。

Try setting session.bug_compat_42 to off, it will turn off the functionality the warning is about. 尝试将session.bug_compat_42设置为off,它将关闭警告所针对的功能。 cf http://php.net/manual/en/session.configuration.php cf http://php.net/manual/zh/session.configuration.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM