什么是$ _REQUEST优先级?

Jus*_*rty 5 php arrays multidimensional-array

PHP超全局变量

PHP具有全局变量,可以在脚本的任何范围内访问.这些变量中的三种($_GET,$_POST,$_COOKIE)被存储的第四变量内($_REQUEST).

$_GET

通过URL参数传递给当前脚本的关联变量数组.

请考虑以下示例,其中发送和访问URL.

http://www.example.com/myPage.php?myVar=myVal
Run Code Online (Sandbox Code Playgroud)
echo $_GET["myVar"]; // returns "myVal"
Run Code Online (Sandbox Code Playgroud)

$_POST

当使用application/x-www-form-urlencoded或multipart/form-data作为请求中的HTTP Content-Type时,通过HTTP POST方法传递给当前脚本的关联变量数组.

使用它的一个例子如下.

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

$_COOKIE

通过HTTP Cookie传递给当前脚本的关联变量数组

setcookie("myVar", "myVal", time() + 3600);
echo $_COOKIE["myVar"]; // returns "myVal"
Run Code Online (Sandbox Code Playgroud)

$_REQUEST

关联数组,默认情况下包含的内容$_GET,$_POST$_COOKIE.


这就是事情

$_REQUEST包含所有三个在一个数组中,并通过访问$_REQUEST["myVar"].

随机场景

让我们假设,不管什么原因,我使用相同的名称为我$_GET,$_POST$_COOKIE.

存储内容的优先级是什么$_REQUEST

假设我通过URL设置发送的数据,通过表单发布,并设置一个彼此相同名称的cookie(一个奇怪的场景,我知道).
可以说我使用了名称"example".

以下输出的输出是什么?

     if ($_REQUEST["example"] == $_GET["example"])    echo "GET";
else if ($_REQUEST["example"] == $_POST["example"])   echo "POST";
else if ($_REQUEST["example"] == $_COOKIE["example"]) echo "COOKIE";
Run Code Online (Sandbox Code Playgroud)

TL;博士

如果$_GET,$_POST和,$_COOKIE都有一个存储有相同名称的值; 哪个人会$_REQUEST以这个名字存储?

Ali*_*aji 6

您在php.ini文件中有一行描述variables_order. 例如:

variables_order = "GPC"
Run Code Online (Sandbox Code Playgroud)

G是 Get,P是 Post,C是 cookie。在这种情况下,第一个 Get 变量设置为$_REQUEST,下一个 Post 变量设置为它,最后一个 Cookie 变量设置为它。所以优先级是cookie,然后是Post,然后是Get。

只要你想改变的值可以更改优先级variables_orderphp.ini文件。

PS:你可以写 G,P,C,S 和 E 的值variables_order。S 代表会话,E 代表环境变量。

PS:在 PHP 5.3 中request_orderphp.ini文件中引入了指令,直接在$_REQUEST数组中设置设置变量的顺序。


num*_*8er 5

php.ini文件中有2个指令:request_ordervariables_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.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)



取自官方文件

  • 我建议将你的评论链接在你的答案中,俗话就是"教人钓鱼". (3认同)