What is the priority of $ _REQUEST?

PHP Supergroup Variables

PHP has global variables that can be accessed in any area of ​​your script. Three of these variables ( $_GET , $_POST , $_COOKIE ) are stored in the fourth variable ( $_REQUEST ).

$_GET

An associative array of variables passed to the current script via URL parameters.

Consider the following example in which a URL is sent and accessed.

 http://www.example.com/myPage.php?myVar=myVal 
 echo $_GET["myVar"]; // returns "myVal" 

$_POST

An associative array of variables passed to the current script through the HTTP POST method when using the / x -www-form-urlencoded or multipart / form-data applications as the HTTP Content-Type in the request.

An example of this is the following.

 <form action="somePage.php" method="POST"> <input type="text" name="myVar" value="myVal" /> <input type="submit" name="submit" value="Submit" /> </form> 
 echo $_POST["myVar"]; // returns "myVal" 

$_COOKIE

An associative array of variables passed to the current script via HTTP cookies

 setcookie("myVar", "myVal", time() + 3600); echo $_COOKIE["myVar"]; // returns "myVal" 

$_REQUEST

An associative array that by default contains the contents of $_GET , $_POST and $_COOKIE .


Here thing

$_REQUEST contains all three in one array and is accessible through $_REQUEST["myVar"] .

Random script

Suppose, for some reason, that I use the same name for my $_GET , $_POST and $_COOKIE .

What will be the priority for what is stored in $_REQUEST .

Assuming I set the submitted data via the URL sent through the form and setting cookies with the same name as the others (weird scenario, I know).
Suppose I used the name "example".

What will be the output of the next output?

  if ($_REQUEST["example"] == $_GET["example"]) echo "GET"; else if ($_REQUEST["example"] == $_POST["example"]) echo "POST"; else if ($_REQUEST["example"] == $_COOKIE["example"]) echo "COOKIE"; 

TL; DR

If $_GET , $_POST and $_COOKIE all have a value stored with the same name; which will $_REQUEST store under the specified name?

+5
source share
2 answers

There are 2 directives in the php.ini file: request_order and variables_order

request_order string

 This directive describes the order in which PHP registers GET, POST and Cookie variables into the _REQUEST array. Registration is done from left to right, newer values override older values. If this directive is not set, variables_order is used for $_REQUEST contents. Note that the default distribution php.ini files does not contain the 'C' for cookies, due to security concerns. 

and

variables_order string

 Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set. If the deprecated register_globals directive is on, then variables_order also configures the order the ENV, GET, POST, COOKIE and SERVER variables are populated in global scope. So for example if variables_order is set to "EGPCS", register_globals is enabled, and both $_GET['action'] and $_POST['action'] are set, then $action will contain the value of $_POST['action'] as P comes after G in our example directive value. 



Adapted from official documentation

+5
source

You have a line in your php.ini file describing variables_order . eg:

 variables_order = "GPC" 

G - Get, P is Post and C - cookie. In this case, with the first Get parameters, the variables are $_REQUEST , the following Post variables are set for them, and the last Cookie variables are set. So, priority is for cookie, then for Post, and then for Get.

You can change the priority as you want by changing the value of variables_order in the php.ini .

PS: You can write G, P, C, S and E in the value of variables_order . S for the session, and E for the environment variables.

PS: In PHP 5.3, the request_order directive is introduced in the php.ini , which sets the order in which variables are set directly in the $_REQUEST .

+4
source

Source: https://habr.com/ru/post/1266207/


All Articles