PHP function import_request_variables () Usage Analysis, importrequest

Source: Internet
Author: User
Tags form post

PHP function import_request_variables () Usage Analysis, importrequest

This article analyzes the usage of the PHP function import_request_variables. We will share this with you for your reference. The details are as follows:

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

Description

bool import_request_variables ( string types [, string prefix])

Import the GET/POST/Cookie variable to the global scope. If you disable register_globals but want to use some global variables, this function is very useful.

You can use the types parameter to specify the variables to be imported. You can use the letters 'G', 'P', and 'C' to indicate GET, POST, and Cookie respectively. These letters are case-insensitive, so you can use any combination of 'g', 'P', and 'c. POST contains the File Information uploaded through the POST method. Note the order of these letters. When "gp" is used, the POST variable overwrites the GET variable with the same name. Any letters other than GPC will be ignored.

Prefix is used as the prefix of the variable name and placed before all variables imported to the global scope. Therefore, 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 specify an empty string as the prefix, you will get an E_NOTICE-level error. Using the default error report level does not display the Notice-level error.

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

You can use the import_request_variables () function to selectively register a global variable set. You can use this function to import values of $ _ GET, $ _ POST, and $ _ COOKIE, and add prefix for each imported variable ).

The types string in the parameter can contain any combination of g, p, c, or 3 characters. "G" indicates the GET variable, "p" indicates the POST variable, and "c" indicates the cookies. Note: The order of the three characters varies. When "pg" is used, the POST variable overwrites the $ _ GET variable with the same name. Otherwise, when "gp" is used, the $ _ GET variable array takes precedence over $ _ POST.

An example of the script for using the import_request_variable () function to import variables is as follows:

// Import the variable value submitted by POST with the prefix post_import_request_variable ("p", "post _"); // import the variable value submitted by GET and POST with the prefix gp _, GET takes precedence over POSTimport_request_variable ("gp", "gp _"); // import the Cookie and GET variable values. Cookie variable values take precedence over GETimport_request_variable ("cg ", "cg _");

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

<? Phpif (isset ($ _ REQUEST ['btn _ submit ']) {echo "normally obtained form POST variable value :". $ _ REQUEST ['username']. "<br/>"; import_request_variables ("pg", "import _"); // display the imported variable name echo "the value of the variable imported using the import_request_variables function :". $ import_Username;}?> <Form id = "test_form" name = "test_form" method = "POST" action = ""> enter your name: <label> <input type = "text" name = "Username" id = "Username"/> </label> <input type = "submit" name = "btn_submit "id =" btn_submit "value =" Submit "/> </label> <br/> </form>

This form prompts you to enter a name. After the name is submitted, the script displays the submitted name in the browser.

Note: The 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 an intermediate method for the following scenarios:

1. When the user cannot use the super variable array;
2. in php. when the register_globals parameter of the ini configuration file is Off (the default version after PHP 5 is Off), use import_request_variables to import the GET/POST/Cookie super variable arrays to the global scope.
3. during development, as long as the scope of the introduced variables is declared, you do not need to write $ _ GET or $ _ REQUEST a bunch of long super Global Array names.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.