在php手册中,这个变量解释为:"默认情况下包含了 $_GET,$_POST 和 $_COOKIE 的数组。"
注意其中包含cookie的内容,做个测试:
2 | $_COOKIE [ 'name' ] = "aaa" ; |
输出结果:
Array ( )
并没有取到cookie的内容呀,为什么呢?
再看手册,下面有一行:
5.3.0 引入 request_order。该指令会影响 $_REQUEST 的内容
我的php版本是5.3.17,打开php.ini,找到request_order:
; This directive determines which super global data (G,P,C,E & S) should
; be registered into the super global array REQUEST. If so, it also determines
; the order in which that data is registered. The values for this directive are
; specified in the same manner as the variables_order directive, EXCEPT one.
; Leaving this value empty will cause PHP to use the value set in the
; variables_order directive. It does not mean it will leave the super globals
; array REQUEST empty.
; Default Value: None
; Development Value: "GP"
; Production Value: "GP"
; http://php.net/request-order
request_order = "GP"
翻译:
这条指令确定了哪些超全局数据该被注册到超全局数组REQUEST中,这些超全局数据包括G(GET),P(POST),C(COOKIE),E(ENV),S(SERVER)。这条指令同样指定了这些数据的注册顺序,换句话说GP和PG是不一样的。注册的顺序是从左至右,即右侧的值会覆盖左侧的。比如,当设置为GPC时,COOKIE > POST > GET,依次覆盖。如果这个项被设置为空,php将会使用指令variables_order的值来指定。
如果将php.ini中的request_order设置为"GPC"后,运行下面的程序post.php,看看结果如何:
03 | setcookie( "value" , "cookie" , time()+3600); |
04 | echo "===GET===<br />" ; |
05 | echo $_GET [ 'value' ] . "<br />" ; |
06 | echo "===POST===<br />" ; |
07 | echo $_POST [ 'value' ] . "<br />" ; |
08 | echo "===COOKIE===<br />" ; |
09 | echo $_COOKIE [ 'value' ] . "<br />" ; |
10 | echo "===REQUEST===<br />" ; |
11 | echo $_REQUEST [ 'value' ] . "<br />" ; |
13 | <form method= "post" action= "?value=get" > |
14 | <input type= "text" name= "value" value= "post" /> |
15 | <input type= "submit" value= "submit" /> |
输出结果:
===GET===
get
===POST===
post
===COOKIE===
cookie
===REQUEST===
cookie
可以看到cookie的值被取到了。
为了使程序兼容更多的版本,不要在程序中使用$_REQUEST这个超全局变量。
当程序确实需要接收get和post过来的参数时,可以用下面的方法代替:
$_REQ = array_merge($_GET, $_POST);
完整代码:
03 | setcookie( "value" , "cookie" , time()+3600); |
05 | $_REQ = array_merge ( $_GET , $_POST ); |
07 | echo "===GET===<br />" ; |
08 | echo $_GET [ 'value' ] . "<br />" ; |
09 | echo "===POST===<br />" ; |
10 | echo $_POST [ 'value' ] . "<br />" ; |
11 | echo "===COOKIE===<br />" ; |
12 | echo $_COOKIE [ 'value' ] . "<br />" ; |
13 | echo "===REQUEST===<br />" ; |
14 | echo $_REQ [ 'value' ] . "<br />" ; |
16 | <form method= "post" action= "?value=get" > |
17 | <input type= "text" name= "value" value= "post" /> |
18 | <input type= "submit" value= "submit" /> |
输出结果:
===GET===
get
===POST===
post
===COOKIE===
cookie
===REQUEST===
post
另外,判断是post还是get请求的页面,最好使用:
复制代码代码示例:
$_SERVER['REQUEST_METHOD']