Home Backend Development PHP Tutorial PHP function import_request_variables() usage and example analysis

PHP function import_request_variables() usage and example analysis

Jun 04, 2018 am 10:38 AM
import request variables

This article mainly introduces the usage of the PHP function import_request_variables(), and analyzes the function, definition and related usage skills of the import_request_variables function in the form of examples. Friends in need can refer to it

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

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), please 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.

<?php
// This will import GET and POST vars
// with an "rvar_" prefix
import_request_variables("gp", "rvar_");
echo $rvar_foo;
?>
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 3 characters. Among them, "g" represents GET variables, "p" represents POST variables, 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. .

The script example of 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:

<?php
if(isset($_REQUEST[&#39;btn_submit&#39;])){
echo "正常取得的表单POST变量值:".$_REQUEST[&#39;Username&#39;]."<br />";
import_request_variables("pg", "import_");
//显示导入的变量名称
echo "使用import_request_variables函数导入的变量值:".$import_Username;
}
?>
<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

This form prompts the user to enter a name. After completion and submission, the script will display the submitted name on 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, suitable for the following situations:

1. When the user cannot use the super variable array;
2. In php.ini 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 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.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

The implementation of PHP obtaining form data and HTML embedding PHP scripts

The method and simplicity of PHP to realize paging display of data A simple analysis of the confusion about length calculation in Example

php

The above is the detailed content of PHP function import_request_variables() usage and example analysis. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x How to use the urllib.request.urlopen() function to send a GET request in Python 3.x Jul 30, 2023 am 11:28 AM

How to use the urllib.request.urlopen() function in Python3.x to send a GET request. In network programming, we often need to obtain data from a remote server by sending an HTTP request. In Python, we can use the urllib.request.urlopen() function in the urllib module to send an HTTP request and get the response returned by the server. This article will introduce how to use

How does Python's import work? How does Python's import work? May 15, 2023 pm 08:13 PM

Hello, my name is somenzz, you can call me Brother Zheng. Python's import is very intuitive, but even so, sometimes you will find that even though the package is there, we will still encounter ModuleNotFoundError. Obviously the relative path is very correct, but the error ImportError:attemptedrelativeimportwithnoknownparentpackage imports a module in the same directory and a different one. The modules of the directory are completely different. This article helps you easily handle the import by analyzing some problems often encountered when using import. Based on this, you can easily create attributes.

How to encapsulate Vue3 Axios interceptor into request file How to encapsulate Vue3 Axios interceptor into request file May 19, 2023 am 11:49 AM

1. Create a new file called request.js and import Axios: importaxiosfrom'axios'; 2. Create a function called request and export it: This will create a function called request and export it Set up a new Axios instance with a base URL. To add timeout settings in a wrapped Axios instance, you can pass the timeout option when creating the Axios instance. exportconstrequest=axios.create({baseURL:'https://example.

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Solution to PHP Notice: Only variables should be passed by reference in Solution to PHP Notice: Only variables should be passed by reference in Jun 22, 2023 pm 09:04 PM

PHP is a commonly used server-side scripting language, so when developing a website, PHPNotice error messages are very common. Among them, "PHPNotice:Onlyvariablesshouldbepassedbyreferencein" is a common error message. What this error message means is: only variables should be passed by reference. We know that in a function or method, variables can be called by passing parameters, so that in the code

What is request in PHP What is request in PHP Jun 01, 2023 am 10:12 AM

Request in PHP refers to request. It is a super global variable in PHP. It is used to collect data submitted by HTML forms and parameters in URLs. It can obtain data from GET and POST requests at the same time. Note that $_request is an associative array. , where the keys are the names of the form fields and the values ​​are the values ​​of the form fields. When using the $_request variable, user-entered data should always be validated and filtered to avoid security issues.

The role and significance of Request in PHP The role and significance of Request in PHP Feb 27, 2024 pm 12:54 PM

The role and significance of Request in PHP In PHP programming, Request is a mechanism for sending requests to the Web server. It plays a vital role in Web development. Request is mainly used to obtain data sent by the client, such as form submission, GET or POST request, etc. Through Request, the data input by the user can be obtained, and the data can be processed and responded to. This article will introduce the role and significance of Request in PHP and give specific code examples.

See all articles