开发者

How to register global variables for php sessions

i get this error:

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开发者_Go百科_compat_warn to off, respectively in Unknown on line 0

According to it, i have to enable(register) the global variables right? How do I do so, and why is the error occurring in the first place?


Are you calling session_start() at the VERY top of your PHP document?

The PHP manual says that the session_register() function has been deprecated as of PHP 5.3.0 and reliance on it is highly discouraged.

Instead, do something like

$_SESSION['dog'] = "Woof";

But you must be calling session_start() before anything else in your document!


after enabling register_globals in PHP directive.

use session_start() at starting of php

$barney = "you can try this";
session_register("barney");

// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["hotel"] = "Starling";

// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["email"] = "neo@gmail.com";


Register globals was a way for variables from _POST/_GET/_SESSION to be defined into global scope under their own name. I've never used or seen usage of variables coming from _SESSION... but the solution is the same. PHP should be announcing the file path & line so all you need to do is refactor your code to stop relying on this deprecated feature.


It seems like register_globals is set to "Off" in you php.ini file. If you have access to it you can change this. This thread is also relevant:

php-session-side-effect-warning-with-global-variables-as-a-source-of-data


Global variables are no longer considered normal use. They can cause all kinds of management issues, memory allocation problems. PHP common practice is to use Session variables instead ($_SESSION["variable_name"]). If you really have your heart set on Globals, you can enable them in php.ini.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜