954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Emulating Register Globals

0
By Bill Anderson on Apr 10th, 2006 12:28 am


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]

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.

eb1024
Newbie Poster
1 post since May 2007
Reputation Points: 10
Solved Threads: 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.

Banderson
Junior Poster in Training
66 posts since Aug 2004
Reputation Points: 17
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You