962148150 发表于 2018-12-20 12:16:28

php中的$_REQUEST

  这是一个细节,真的很细。
  我们查一下php手册,上面会清清楚楚的告诉你,这个变量是"默认情况下包含了 $_GET,$_POST 和 $_COOKIE 的数组。"
  原来里面有cookie的内容呀,于是,做了一个测试:


[*]$_COOKIE['name'] = "aaa";
[*]print_r($_REQUEST);

  输出结果:


[*]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来看一下结果吧:


[*]
[*]
[*]   
[*]   
[*]

  点了提交按钮后会出现下面的结果:


[*]===GET===
[*]get
[*]===POST===
[*]post
[*]===COOKIE===
[*]cookie
[*]===REQUEST===
[*]cookie

  可以看到cookie的值被取到了。为了使程序兼容更多的版本,不要在程序中使用$_REQUEST这个超全局变量。当我们的程序确实需要接收get和post过来的参数时,我们可以用下面的方法来代替:


[*]$_REQ = array_merge($_GET, $_POST);

  完整的程序如下:


[*]
[*]
[*]   
[*]   
[*]

  当点击提交后,输出结果如下:


[*]===GET===
[*]get
[*]===POST===
[*]post
[*]===COOKIE===
[*]cookie
[*]===REQUEST===
[*]post

  另外,要判断用户到底是post还是get请求的页面,最好是用


[*]$_SERVER['REQUEST_METHOD']

  来判断。



页: [1]
查看完整版本: php中的$_REQUEST