SoFunction
Updated on 2025-03-09

Analysis of usage of PHP function import_request_variables

This article analyzes the usage of PHP function import_request_variables(). Share it for your reference, as follows:

The import_request_variables function can import the GET/POST/Cookie variable into the global scope when register_global = off.

describe

bool import_request_variables ( string types [, string prefix])

Import the GET/POST/Cookie variable into the global scope. This function is useful if you ban register_globals but want to use some global variables.

You can use the types parameter to specify the variables to be imported. GET, POST and cookies can be represented by the letters 'G', 'P' and 'C', respectively. These letters are case-insensitive, so you can use any combination of 'g', 'p' and 'c'. POST contains file information uploaded through the POST method. Note the order of these letters, when using "gp", the POST variable will override the GET variable with the same name. Any letters other than GPC will be ignored.

The prefix parameter is used as the prefix for the variable name and is placed before all variables imported into the global scope. So if you have a GET variable named "userid" and provide "pref_" as the prefix, you will get a global variable named $pref_userid.

If you are interested in importing other global variables, such as SERVER variables, consider using extract().

Note: Although the prefix parameter is optional, if you do not specify a prefix, or if you specify an empty string as the prefix, you will get an E_NOTICE level error. Using the default error reporting level does not display errors at the Notice level.

<?php
// This will import GET and POST vars
// with an "rvar_" prefix
import_request_variables("gp", "rvar_");
echo $rvar_foo;
?>

Use the import_request_variables() function to selectively register a collection of global variables. You can use this function to import the values ​​of $_GET, $_POST, and $_COOKIE, and you can also prefix each imported variable (prefix).

The type string in the parameter is allowed to be g, p, c characters, or any combination between 3 characters. Where "g" represents the GET variable, "p" represents the POST variable, and "c" represents the cookies. Note: There are differences in the order of the arrangement of the 3 characters. When using "pg", the POST variable will override the $_GET variable with the same name; conversely, when using "gp", the $_GET variable array will take precedence over $_POST.

Examples of scripts that use the import_request_variable() function to implement variable import are as follows:

//Import the variable value submitted by POST, with the prefix as post_import_request_variable("p", "post_");
//Import the variable values ​​submitted by GET and POST, with the prefix of gp_, and GET takes precedence over POSTimport_request_variable("gp", "gp_");
//Import the variable values ​​of cookies and GETs, and the value of the cookie variables takes precedence over GETsimport_request_variable("cg", "cg_");

If we use "pg parameter" in the import_request_variables() function, please see the following script example:

&lt;?php
if(isset($_REQUEST['btn_submit'])){
echo "Normal acquisition of form POST variable value:".$_REQUEST['Username']."&lt;br /&gt;";
import_request_variables("pg", "import_");
//Show the imported variable nameecho "Variable value imported using import_request_variables function:".$import_Username;
}
?&gt;
&lt;form  name="test_form" method="POST" action=""&gt;
Please enter your name:
&lt;label&gt;
&lt;input type="text" name="Username"  /&gt;
&lt;/label&gt;
&lt;label&gt;
&lt;input type="submit" name="btn_submit"  value="submit" /&gt;
&lt;/label&gt;
&lt;br /&gt;
&lt;/form&gt;

The form prompts the user to enter a name. After completing and submitting, the script will display the submitted name on the browser.

Note: The prefix prefix parameter is required. If the prefix is ​​not specified, or an empty string is specified as the variable prefix, PHP will throw an E_NOTICE error.

The import_request_variables() function provides us with an intermediate method, which is suitable for the following situations:

1. When the user cannot use the super variable array;
2. When the register_globals parameter of the configuration file is Off (the default is Off for versions after PHP 5), use import_request_variables to import the array of super variables such as GET/POST/Cookie into the global scope.
3. During development, as long as the introduced variable range is declared, there is no need to write a bunch of long super global array names of $_GET or $_REQUEST.

For more information about PHP related content, please check out the topic of this site:Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.