PHP 超全局变量
PHP 具有可以在脚本的任何范围内访问的全局变量。其中三个变量($_GET
、$_POST
、$_COOKIE
)存储在第四个变量($_REQUEST
>).
$_GET
An associative array of variables passed to the current script via the URL parameters.
考虑以下发送和访问 URL 的示例。
http://www.example.com/myPage.php?myVar=myVal
echo $_GET["myVar"]; // returns "myVal"
$_POST
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
使用的示例如下。
<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
.
事情是这样的
$_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";
tl;dr
如果$_GET
、$_POST
和$_COOKIE
都存储了同名的值; $_REQUEST
将以所述名称存储哪一个?
最佳答案
在 php.ini
文件中有 2 个指令:request_order
和 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.
和
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.
关于php - $_REQUEST 优先级是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43157933/