Emulating Register Globals

Banderson 0 Tallied Votes 426 Views Share


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]
eb1024 0 Newbie Poster

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.

Banderson 29 Junior Poster in Training

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.