使用全局变量作为数据源的 PHP 会话副作用警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/175091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP session side-effect warning with global variables as a source of data
提问by Zack Peterson
I'm trying to host a PHP web site that was given to me. I see this warning:
我正在尝试托管一个给我的 PHP 网站。我看到这个警告:
Warning:Unknown: Your script possibly relies on a session side-effect which existed until 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. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknownon line 0
警告:未知:您的脚本可能依赖于 PHP 4.2.3 之前存在的会话副作用。请注意,除非启用了 register_globals,否则会话扩展不会将全局变量视为数据源。您可以通过分别将 session.bug_compat_42 或 session.bug_compat_warn 设置为 off 来禁用此功能和此警告。在未知的第0行
What does this mean? How might I track down the source of this problem within the code?
这是什么意思?我如何在代码中追踪这个问题的根源?
回答by Owen
basically you have a variable with the same name as your session. ex:
基本上你有一个与你的会话同名的变量。前任:
$_SESSION['var1'] = null;
$var1 = 'something';
which will reproduce this error. you can stop PHP from trying to find existing variables and warning you about them by adding these lines to your script:
这将重现此错误。您可以通过将这些行添加到脚本中来阻止 PHP 尝试查找现有变量并警告您:
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);
these values can be set in php.ini or .htaccess as well
这些值也可以在 php.ini 或 .htaccess 中设置
回答by Kzqai
There seem to be a few problematic possibilities here:
这里似乎有一些有问题的可能性:
http://www.spiration.co.uk/post/1231/Your-script-possibly-relies-on-a-session-side-effect
http://www.spiration.co.uk/post/1231/Your-script-possably-relies-on-a-session-side-effect
says that cases like this:
说这样的情况:
$_SESSION['firstname']=$_REQUEST['firstname'];
will trigger the warning.
会触发警告。
Additionally, I interpret this php bug content: http://bugs.php.net/bug.php?id=41540to mean that this error may also occur when you assign a variable to the session superglobal that is not yet initialized, e.g.
另外,我解释这个 php 错误内容:http: //bugs.php.net/bug.php?id=41540意味着当您将变量分配给尚未初始化的会话超全局变量时,也可能发生此错误,例如
//Start of script
$_SESSION['bob'] = $bob;
回答by Ian
This is good information on finding out what's causing the warning, but I would recommend NOT shutting off the warnings Owen mentions. These runtime functions are removed in PHP 5.4.0and the developer should get into the practice of avoiding such usage of variables.
这是找出导致警告的原因的好信息,但我建议不要关闭 Owen 提到的警告。这些运行时函数在 PHP 5.4.0 中被删除,开发人员应该避免这种变量的使用。
To fix this, it may be a pain on the developers end, but if you have
为了解决这个问题,开发人员可能会很痛苦,但如果你有
$_SESSION["user"]
$user;
rename the session to
将会话重命名为
$_SESSION["sessuser"];
Or vise-versa just as long as the session name and the variable name are different. Think of it this way: when you upgrade to the latest build, you'll have to debug your code anyhow.
或者反之亦然,只要会话名称和变量名称不同。可以这样想:当您升级到最新版本时,无论如何都必须调试代码。
回答by Praveen Kannan
When you are making changes to the .htaccess ini_set does not work. You will need to do it as:
当您对 .htaccess 进行更改时,ini_set 不起作用。你需要这样做:
php_flag session.bug_compat_42 0
php_flag session.bug_compat_warn 0
回答by TARA
in my case, php.ini change from on to off
在我的情况下,php.ini 从打开变为关闭
like this :
像这样 :
session.bug_compat_42 = off
session.bug_compat_warn = off
if not working, restart apache
如果不起作用,请重新启动apache

