如何在 PHP 5.4 或更高版本中模拟 register_globals?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16706098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:34:23  来源:igfitidea点击:

How can I emulate register_globals in PHP 5.4 or newer?

phpregister-globals

提问by LX7

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

我正在开发一个使用register_globals. 我的本地 php 版本是 5.4。

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

我知道register_globals自 PHP 5.3.0 起已弃用并在 PHP 5.4 中删除,但我必须使此代码在 PHP 5.4 上运行。

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

有没有办法在较新版本的 PHP 上模拟功能?

回答by sectus

You can emulate register_globalsby using extractin global scope:

您可以register_globals通过在全局范围内使用提取来模拟:

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.phpfile with

如果您有一个已发布的应用程序并且不想更改其中的任何内容,则可以使用以下命令创建globals.php文件

<?php
extract($_REQUEST);

then add auto_prepend_filedirective to .htaccess (or in php.ini)

然后将auto_prepend_file指令添加到 .htaccess(或在 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.

在此之后,代码将在每次调用时运行,应用程序将像旧设置一样工作。

回答by Ernesto Allely

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

以防万一它可能会有所帮助,这是 php.net 上建议的用于模拟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

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

回答by rink.attendant.6

From the PHP manual, it says that:

PHP 手册中,它说:

This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

此功能自 PHP 5.3.0 起已弃用,自 PHP 5.4.0 起已移除。

However, a Google search revealed this method on Ubuntu Forums:

然而,谷歌搜索在Ubuntu 论坛上发现了这种方法:

Nope, it's finally gone for good. Whatever site is still using globals had, what, half-a-dozen years or more now to fix the code?

The quickest solution is to create the globals from scratch by running this code at the beginning of the app:

Code:

不,它终于永远消失了。哪个站点仍然使用全局变量,现在已经有六年或更长时间来修复代码了?

最快的解决方案是通过在应用程序的开头运行以下代码从头开始创建全局变量:

代码:

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

You have to be careful that any variable created this way isn't already defined in the remainder of the script.

You can force this code to be run ahead of every page on the site by using the auto_prepend_file directive in an .htaccess file.

您必须小心,以这种方式创建的任何变量尚未在脚本的其余部分中定义。

您可以通过在 .htaccess 文件中使用 auto_prepend_file 指令来强制此代码在站点上的每个页面之前运行。

I'd strongly recommend looking at the code that requires register_globalsand changing it so that it works properly with it being disabled.

我强烈建议查看需要的代码register_globals并对其进行更改,以便它在被禁用的情况下正常工作。

回答by user3081809

In php.ini before:

在 php.ini 之前:

auto_globals_jit = On

After:

后:

auto_globals_jit = Off