Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I emulate register_globals in PHP 5.4 or newer?

I am working on a framework that uses register_globals. My local php version is 5.4.

I know register_globals is deprecated since PHP 5.3.0 and removed in PHP 5.4, but I have to make this code work on PHP 5.4.

Is there any way to emulate the functionality on newer versions of PHP?

like image 734
LX7 Avatar asked May 23 '13 05:05

LX7


2 Answers

You can emulate register_globals by using extract in global scope:

extract($_REQUEST);

Or put it to independent function using global and variable variables

function globaling()
{
    foreach ($_REQUEST as $key => $val)
    {
        global ${$key};
        ${$key} = $val;
    }
}

If you have a released application and do not want to change anything in it, you can create globals.php file with

<?php
extract($_REQUEST);

then add auto_prepend_file directive to .htaccess (or in php.ini)

php_value auto_prepend_file ./globals.php

After this the code will be run on every call, and the application will work as though the old setting was in effect.

like image 186
sectus Avatar answered Sep 18 '22 19:09

sectus


Just in case it may be helpful, this is the code suggested on php.net to emulate register_globals On:

<?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);
    }
}

Source: http://php.net/manual/en/faq.misc.php#faq.misc.registerglobals

like image 27
Ernesto Allely Avatar answered Sep 19 '22 19:09

Ernesto Allely