downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

intval> <gettype
[edit] Last updated: Fri, 07 Jun 2013

view this page in

import_request_variables

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

import_request_variablesImportă variabilele GET/POST/Cookie în circumstanța globală

Descrierea

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

Importă variabilele GET/POST/Cookie în circumstanța globală. Această funcție este utilă dacă ați dezactivat register_globals, însă doriți să aveți acces la unele variabile din circumstanța globală.

Dacă vă interesează importul altor variabile în circumstanța globală, cum ar fi $_SERVER, considerați utilizarea extract().

Parametri

types

Utilizând parametrul types puteți specifica care variabile de interpelare vor fi importate. Puteți utiliza caracterele 'G', 'P' și 'C' respectiv pentru GET, POST și Cookie. Nu importă dacă caracterele sunt majuscule sau minuscule, de aceea puteți utiliza orice combinație cu 'g', 'p' și 'c'. POST include și informația despre fișierele încărcate prin metoda POST.

Notă:

Observați că ordinea literelor contează, de aceea la utilizarea "GP" variabilele POST vor acoperi variabilele GET cu același nume. Orice alte litere decât GPC sunt ignorate.

prefix

Prefixul denumirilor variabilelor, plasat înaintea denumirilor tuturor variabilelor importate în circumstanța globală. Deci dacă aveți o valoare GET denumită "userid" și indicați prefixul "pref_", atunci veți obține o variabilă globală denumită $pref_userid.

Notă:

Cu toate că parametrul prefix este opțional, veți obține o eroare de nivel E_NOTICE dacă nu specificați un prefix, sau specificați un string vid în calitate de prefix. Acesta probabil că nu este un pericol de securitate. Erorile de nivel notice nu sunt afișate la nivelul de raportare al erorilor implicit.

Valorile întoarse

Întoarce valoarea TRUE în cazul succesului sau FALSE în cazul eșecului.

Exemple

Example #1 Exemplu import_request_variables()

<?php
// Aceasta va importa variabilele GET și POST
// și le va prefixa cu "rvar_"
import_request_variables("gp""rvar_");

echo 
$rvar_foo;
?>

Vedeți de asemenea



intval> <gettype
[edit] Last updated: Fri, 07 Jun 2013
 
add a note add a note User Contributed Notes import_request_variables - [9 notes]
up
2
cornflake4 at gmx dot at
8 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
up
2
brian at enchanter dot net
8 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
samb06 at gmail dot com
7 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
1
ceo AT l-i-e DOT com
8 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
1
rustamabd at gmail dot com
1 year 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
0
jason
7 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
0
nexxer at rogers dot com
8 years ago
PHP5 seems to have fixed that, in the sense that import_request_variables("g") works like extract($_GET). It doesn't seem to be passing a reference to the global, but instead creating a copy of it as expected
up
-4
cornflake4 at gmx dot at
8 years ago
Beware:

import_request_variables() does not copy the request variables into local scope variables. Instead, it copies the *reference* to the request variable content to local variables Important implication: any change to the local variable means a changes to the respective request variable, too!

This is a clear difference to extract($_GET) which copies the content of the request variables into local variables.

To shed some light on the implication, consider this (assuming the query string "...&var=1"):

echo $_GET['var']; # prints: 1
import_request_variables();
echo $var; # prints 1
$var = 2;
echo $_GET['var']; # prints 2 !!!!

So, $_GET has changed as well!

On the other hand:

echo $_GET['var']; # prints: 1
extract($_GET);
echo $var; # prints 1
$var = 2;
echo $_GET['var']; # prints 2 !!!!

Because of this, I recommend NOT using import_request_variables(), but extract($_GET); extract($_POST); extract($_COOKIE); instead, since this combination bears not these unexspected side effects.

PS: not to mention that you have to reconsider your coding style if any need to import_request_variables arises at all!
up
-5
michal dot kocarek at NO_SPAM dot seznam dot cz
5 years ago
Regarding the last post:

When $_GET[$k] is compared against NULL or '' (empty string) inside the foreach loop, it should be compared only against one of the expressions or strict equality operator should be used.
In this case, second part of expression ($_GET[$k] == NULL) will be *never* executed, because of NULL gets converted to empty string.
Also be aware that zero is equal (==) to empty string, so if passing zeroes through the $_GET, use strict comparsion to check whether variable exist or not.
Next notice: when nothing will be set into $_GET array, all comparsions will generate lot of E_NOTICE errors, because you are accessing unassigned variable.

<?
// Slightly modified previous example
$input = array('name' => null, 'age' => 26) ;
// 26 is the default age, if $_GET['age'] is empty or not set

/**
 * Extracts $_GET variables to global scope by the definition from the $input array
 * @return void
 */
function extract_get() {
    global $input;
   
    if (isset($input) && is_array($input)) foreach ($input as $k => $v) {
        if (!isset($_GET[$k])) {
            $GLOBALS[$k] = $v;
            continue;
        }
        $getval = $_GET[$k];
        if ($getval === null || $getval === '') {
            $getval = $v;
        } elseif (is_numeric($v)) {
            $getval = (int) $getval;
        } elseif (get_magic_quotes_gpc() == 1) {
            $getval = stripslashes_deep($getval);
        }
        $GLOBALS[$k] = $getval;
        unset($getval);
    }
}

/**
 * Performs stripslashes function recursively on the array or on the single variable
 * @param mixed $var Variable - can be scalar variable or the array
 * @return mixed Variable with slashes stripped with function stripslashes()
 */
function stripslashes_deep($var) {
    if (!is_array($var))
        return stripslashes($var);
    foreach($var as $k => $v) {
        $var[$k] = stripslashes_deep($v);
    }
    return $var;
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites