php function Import_request_variables () usage Analysis _php technique

Source: Internet
Author: User
Tags form post

This article analyzes the PHP function Import_request_variables () usage. Share to everyone for your reference, specific as follows:

The Import_request_variables function can import Get/post/cookie variables 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 you want to use some global variables.

You can use the types parameter to specify the variables you want to import. You can use the letters ' G ', ' P ' and ' C ' respectively to represent get, POST, and cookies. These letters are not case-sensitive, so you can use any combination of ' g ', ' P ' and ' C '. The post contains the file information uploaded through the Post method. Note The order of these letters, and when you use "GP", the POST variable overwrites the get variable with the same name. Any letters other than GPC will be ignored.

The prefix parameter is prefixed to the variable name and is placed before all variables that are imported into the global scope. So if you have a get variable named "userid" and you provide "pref_" as a 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, you will get a e_notice level error if you do not specify a prefix, or if you specify an empty string as the prefix. Using the default error reporting level is an error that does not show the attention (Notice) level.

<?php
//This'll 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 global variable collection. You can use this function to import values for $_get, $_post, and $_cookie, and you can add a prefix (prefix) to each imported variable.

The types string in the argument allows for G, p, c characters, or any combination of 3 characters. where "G" denotes a Get variable, "p" denotes a post variable, and "C" denotes a cookie. Note: The order of 3 characters is different, and when "PG" is used, the POST variable overwrites the $_get variable with the same name, whereas the $_get variable array takes precedence over $_post when the GP is used.

Examples of scripts that implement variable imports using the Import_request_variable () function are as follows:

Import the value of the variable submitted by post, prefixed by Post_
import_request_variable ("P", "Post_");
Import the variable value of get and post submitted, prefixed by Gp_,get prior to post
import_request_variable ("GP", "Gp_");
Import the variable value of the cookie and get, and the cookie variable value takes precedence over get
import_request_variable ("CG", "cg_");

If we use the PG parameter in the Import_request_variables () function, consider the following script instance:

<?php
if (isset ($_request[' Btn_submit ')) {
echo "the form post variable value normally obtained:". $_request[' Username ']. " <br/> ";
Import_request_variables ("PG", "Import_");
Displays the imported variable name
echo "Variable value imported using the Import_request_variables function:". $import _username;
>
<form id= "Test_form" name= "Test_form" method= "POST" action= "" >
Please enter your name:
<label>
<input type= "text" name= "Username" id= "Username"/>
</label>
<label>
< Input type= "Submit" Name= "Btn_submit" id= "Btn_submit" value= "Submit"/>
</label>
<br
/> </form>

The form prompts the user for a name, and when it is completed and submitted, the script displays the submitted name on the browser.

Note: The prefix prefix parameter is required, and if you do not specify a prefix, or if you specify an empty string as the variable prefix, PHP throws a e_notice error.

The Import_request_variables () function provides us with an intermediate method that applies to the following situations:

1. When the user cannot use the super variable array;
2. When the register_globals parameter of the php.ini profile is off (the version after PHP 5 defaults to OFF), using Import_request_variables will get/post/ Cookies are imported into the global scope by a few super variable arrays.
3. At development time, you do not have to write $_get or $_request a bunch of very long super global array names as long as you declare the range of variables introduced.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.