php中$_REQUEST、 $_GET、 $_POST、 $_COOKIE 的关系和区别

看到REQUEST可以通吃GET 、POST 、COOKIE 后 感觉这个$_REQUEST太强大了

是不是其他的几个超级变量就没有用了,下面对他们整体做个比较:

1.安全性 post>get

2.数据传输大小 post>get(get最大2000字节)

3.保存到收藏夹 get比较方便.

4.权限大小

首先权限大小跟php.ini文件有关

; This directive determines which super global data (G,P & C) 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"

  

这里的意思是:

; 是否将EGPCS变量注册成为全局变量.

; 如果你不希望由于用户数据而导致你脚本的全局变量变得凌乱,你需要关闭此选项

; 这个一般随着 track_vars 打开 - 在这种情况下你能够通过$HTTP_*_VARS[]存取所有的GPC变量.

这些超全局数据包括G(GET),P(POST),C(COOKIE),E(ENV),S(SERVER)。这条指令同样指定了这些数据的注册顺序,换句话说GP和PG是不一样的。注册的顺序是从左至右,即右侧的值会覆盖左侧的。比如,当设置为GPC时,COOKIE > POST > GET,依次覆盖。如果这个项被设置为空,php将会使用指令variables_order的值来指定。

在$_POST['name'] = 'post', $_GET ['name'] = 'get' ,setcookie('name', 'cookie', time()+3600);//先把COOKIE种上,名字为name,值为cookie。然后刷新。(COOKIE要刷新才生效)

上面三个不同的全局变量中键名都是相同的

但是print_r($_REQUEST); 后查看结果 'cookie'。

结论:COOKIE的优先级最高。下来POST,最后才是GET

权限大小比较:$_COOKIE > $_POST > $_GET

另外,判断是post还是get请求的页面,最好使用:$_SERVER['REQUEST_METHOD']