Home Backend Development PHP Tutorial PHP function import_request_variables() usage analysis_php skills

PHP function import_request_variables() usage analysis_php skills

May 16, 2016 pm 07:55 PM
php function

This article analyzes the usage of PHP function import_request_variables() with examples. Share it with everyone 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])

Copy after login

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

You can use the types parameter to specify the variables that need to be imported. The letters 'G', 'P' and 'C' can be used to represent GET, POST and Cookie respectively. These letters are not case-sensitive, 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 overwrite the GET variable with the same name. Any letters outside of GPC will be ignored.

The

prefix parameter serves as the prefix of 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 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, if you do not specify a prefix, or specify an empty string as a prefix, you will get an E_NOTICE level error. Note level errors are not displayed using the default error reporting level.

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

Copy after login

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. You can also add a prefix to each imported variable.

The type string in the parameter allows g, p, c characters, or any combination of the three characters. Among them, "g" represents the GET variable, "p" represents the POST variable, and "c" represents cookies. Note: There is a difference in the order of the 3 characters. When "pg" is used, the POST variable will overwrite the $_GET variable with the same name; conversely, when "gp" is used, the $_GET variable array will take precedence over $_POST. .

A script example using the import_request_variable() function to implement variable import is as follows:

//导入POST提交的变量值,前缀为post_
import_request_variable("p", "post_");
//导入GET和POST提交的变量值,前缀为gp_,GET优先于POST
import_request_variable("gp", "gp_");
//导入Cookie和GET的变量值,Cookie变量值优先于GET
import_request_variable("cg", "cg_");

Copy after login

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

<&#63;php
if(isset($_REQUEST['btn_submit'])){
echo "正常取得的表单POST变量值:".$_REQUEST['Username']."<br />";
import_request_variables("pg", "import_");
//显示导入的变量名称
echo "使用import_request_variables函数导入的变量值:".$import_Username;
}
&#63;>
<form id="test_form" name="test_form" method="POST" action="">
请输入您的名字:
<label>
<input type="text" name="Username" id="Username" />
</label>
<label>
<input type="submit" name="btn_submit" id="btn_submit" value="提交" />
</label>
<br />
</form>

Copy after login

The form prompts the user to enter a name. Once completed and submitted, the script will display the submitted name in the browser.

Note: The prefix parameter is required. If no prefix is ​​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 in the php.ini configuration file is Off (the default value for versions after PHP 5 is Off), use import_request_variables to import the GET/POST/Cookie super variable arrays into the global scope.
3. During development, as long as the introduced variable scope is declared, there is no need to write a bunch of long super global array names in $_GET or $_REQUEST.

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", " Introductory tutorial on PHP basic syntax", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" And "Summary of common database operation skills in PHP"

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles