0


Ever since PHP turned register_globals_off I have had problems here and there in my codes. I searched hard and long for a simple fix while keeping globals turned off. Fortunately, I found one! I have posted it below for your convenience and it is also described Here.


I'm not sure if this has ever been posted, but if not I hope it helps you as much as it did me. :cool:

Here is the code to emulate register_globals On. 

[CODE]
<?php
// Emulate register_globals on
if (!ini_get('register_globals')) {
    $superglobals = array($_SERVER, $_ENV,
        $_FILES, $_COOKIE, $_POST, $_GET);
    if (isset($_SESSION)) {
        array_unshift($superglobals, $_SESSION);
    }
    foreach ($superglobals as $superglobal) {
        extract($superglobal, EXTR_SKIP);
    }
    ini_set('register_globals', true);
}
?> [/CODE]


And this code will emulate register_globals Off. 

[CODE]
<?php
// Emulate register_globals off
if (ini_get('register_globals')) {
    $superglobals = array($_SERVER, $_ENV,
        $_FILES, $_COOKIE, $_POST, $_GET);
    if (isset($_SESSION)) {
        array_unshift($superglobals, $_SESSION);
    }
    foreach ($superglobals as $superglobal) {
        foreach ($superglobal as $global => $value) {
            unset($GLOBALS[$global]);
        }
    }
    ini_set('register_globals', false);
}
?> 
[/CODE]
2
Contributors
2
Replies
13
Views
11 Years
Discussion Span
Last Post by Banderson
-1

To emulate register_globals = on one could just use

<?php extract($_REQUEST); ?>

and, register_globals = off is not being emulated the right way in your snippet.

0

I use this snippet in all my codes if I have problems postings or getting vars. It works every time for me. I have made this into a class and used it for a long time.

Now why do you say :
and, register_globals = off is not being emulated the right way in your snippet.

Some people may use $_REQUEST in their codes which is fine, but as long as you code with security in mind and you know where the vars and methods are coming from $_REQUEST does not need to be used.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.