PHP Velho Oeste 2024

import_request_variables

(PHP 4 >= 4.1.0, PHP 5 < 5.4.0)

import_request_variablesImportar variables GET/POST/Cookie en el ámbito global

Descripción

import_request_variables ( string $types [, string $prefix ] ) : bool

Importa las variables GET/POST/Cookie en el ámbito global. Esto es útil si ha deshabilitado register_globals, pero le gustaría ver algunas variables en el ámbito global.

Si está interesado en importar otras variables en el ámbito global, como $_SERVER, considere el uso de extract().

Advertencia

Esta función ha sido declarada OBSOLETA desde PHP 5.3.0 y ELIMINADA a partir de PHP 5.4.0.

Parámetros

types

Usando el parámetro types, puede especificar cuáles variables de petición deben importarse. Puede usar los caracteres 'G', 'P' y 'C' respectivamente para indicar GET, POST y Cookie. Estos caracteres no son sensibles a mayúsculas o minúsculas, por lo que también puede utilizar cualquier combinación de 'g', 'p' y 'c'. POST incluye la información de archivos cargados mediante POST.

Nota:

Tenga en cuenta que el orden de las letras es importante, ya que cuando se utiliza "GP", las variables POST sobrescribirán las variables GET con el mismo nombre. Cualquier otra letra diferente a GPC es descartada.

prefix

El nombre de variable prefijo, puede ser colocado antes de nombrar a todas las variables importadas en el ámbito global. De modo que si tiene un valor GET llamado "userid", y proporciona un prefijo "pref_", entonces obtendrá una variable global llamada $pref_userid.

Nota:

Aunque el parámetro prefix es opcional, recibirá un error de nivel E_NOTICE si no especifica un prefijo, o especifica una cadena vacía como prefijo. Este es un riesgo potencial de seguridad. La notificación de aviso de errores no es mostrada usando el nivel predeterminado de reporte de errores.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de import_request_variables()

<?php
// Esto importará las variables GET y POST con el prefijo "rvar_"

import_request_variables("gp""rvar_");

echo 
$rvar_foo;
?>

Ver también

add a note add a note

User Contributed Notes 6 notes

up
8
ceo AT l-i-e DOT com
19 years ago
Call me crazy, but it seems to me that if you use this function, even WITH the prefix, then you might as well just turn register_globals back on...

Sooner or later, somebody will find a "hole" with your prefixed variables in an un-initialized variable.

Better to import precisely the variables you need, and initialize anything else properly.
up
5
brian at enchanter dot net
19 years ago
import_request_variables does *not* read from the $_GET, $_POST, or $_COOKIE arrays - it reads the data directly from what was submitted. This is an important distinction if, for example, the server has magic_quotes turned on and you massage the data to run stripslashes on it; if you then use import_request_variables, your variables will still have slashes in them.

In other words: even if you say $_GET=""; $_POST=""; then use import_request_variables, it'll still get all the request data.

If you change the contents of $_GET and you then want to bring this data into global variables, use extract($_GET, EXTR_PREFIX_ALL, "myprefix") instead.
up
1
rustamabd at gmail dot com
12 years ago
import_request_variables() is gone from PHP since version 5.4.0. A simple plug-in replacement it extract().

For example:

import_request_variables('gp', 'v_');

Can be replaced with:

extract($_REQUEST, EXTR_PREFIX_ALL|EXTR_REFS, 'v');
up
-1
jason
18 years ago
reply to ceo AT l-i-e DOT com:

I don't think it's a risk, as all of your request variables will be tagged with the prefix. As long as you don't prefix any of your internal variables with the same, you should be fine.

If someone tries to access an uninitiated security-related variable like $admin_level through request data, it will get imported as $RV_admin_level.
up
-2
samb06 at gmail dot com
18 years ago
What i do is have a small script in my header file that takes an array called $input, and loops through the array to extract variables. that way the security hole can be closed, as you specify what variables you would like extracted

$input = array('name' => null, 'age' => 26) ;

// 26 is the default age, if $_GET['age'] is empty or not set

function extract_get()
    {
        global $input ;
       
        if ($input)
            {
                foreach ($input as $k => $v)
                    {
                        if ($_GET[$k] == '' or $_GET[$k] == NULL)
                            {
                                $GLOBALS[$k] = $v ;
                            }
                        else
                            {
                                $GLOBALS = $_GET[$k] ;
                            }
                    }
            }
    }
up
-3
cornflake4 at gmx dot at
19 years ago
oops, a typo in my comment:

The last line in the second example (the on using the extract() function) should read:

echo $_GET['var']; # prints 1, so $_GET has been unchanged
To Top