background image

php 中使用$_REQUEST 注意事项

本文主要介绍了,php 中使用$_REQUEST 需要注意的一个问题,供大家参考下
问题
说起$_REQUEST,大家都知道的是它是$_GET 和$_POST 的集合。但是如果你有心的话,
查一下文档,会看到:
$_REQUEST
An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
这里说$_REQUEST 默认是$_GET, $_POST, $_COOKIE 的集合,结果我使用我本地的 php
查看了一下发现只有$_GET, $_POST, 没有$_COOKIE

 

!! 难道文档是错的?

答案
其实 changelog 中有给出解释:

版 本 5.3 以 上 , php.ini 中 有 request_order 属 性 来 设 置 $_REQUEST 。 查 了 下 php.ini , 
request_order 设置成为了 GP(Get and Post)。
request_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.
原来是 G,P,C 分别代表 Get,Post,Cookie,5.3 以上的版本 request_order 默认是设置成
GP 的,并不包含 C,即$_REQUEST 默认只包含$_GET 和$_POST !! (所以官网文档有一
定的误导)。
也同时说一下 G,P,C 的先后顺序就是设置的 array 的覆盖顺序。
提醒下如果你是使用 fpm-php 实验的话,改了 php.ini 后你需要重启 php-fpm