【问题标题】:what $_REQUEST will return when same values sent by GET and POST?当 GET 和 POST 发送相同的值时,$_REQUEST 将返回什么?
【发布时间】:2015-10-29 03:22:28
【问题描述】:

我正在尝试在PHP 中创建一个表单,我使用用户 ID 通过 GET 在表单中显示数据,但是在通过 POST 提交表单后,我将用户 ID 存储在隐藏字段中..

在尝试此操作时,我只是对GETPOSTREQUEST 感到困惑。

看到这种情况

<form action="script.php?id=777" method="post">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

假设我在文本字段中输入“888” 什么时候提交这个表单,$_REQUEST['id']; 应该提供什么值?

所有php versions都一样吗?

如果我将文本字段留空会怎样?

如果我将操作更改为action="script.php?id=",会发生什么?

【问题讨论】:

  • 你最后不能检查这个吗?需要多少时间?
  • 它有时会出人意料..我只是想确定它是否有任何规则。这样我就可以在项目中使用它之前依赖它。
  • 最好不要使用$_REQUEST。为什么 - stackoverflow.com/questions/2142497/…

标签: php forms post get


【解决方案1】:

01

如果表单在post方法中

<form action="script.php" method="post">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

script.php,可以通过使用获取数据

$id = $_POST['id'];//works
$id = $_REQUEST['id'];//works
$id = $_GET['id'];//Failed

02

如果表单是get 方法

<form action="script.php" method="get">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

script.php,可以通过使用获取数据

$id = $_GET['id'];//works
$id = $_REQUEST['id'];//works
$id = $_POST['id'];//Failed

您可以参考$_REQUEST vs $_GET and $_POSTWhat's wrong with using $_REQUEST[]?

【讨论】:

  • Thnx,但我只是想知道如果'?id=1' 在 url 中并且id = 2 是通过 POST 发送的,那么$_REQUEST[''id] = ?
  • 好吧,这意味着我不能相信它总是会给=1,因为我有时会尝试它给=2..
【解决方案2】:

实际顺序在 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"

通常默认设置是获取然后发布。在这种情况下,您将 id 参数作为 get 和作为 post 参数提供。这意味着 $_REQUEST 先填充 $_GET,然后填充 $_POST。这意味着 $_REQUEST 将反映 $_POST。

【讨论】:

  • Thnx @Ronald.. 这就是为什么它在不同系统中的工作方式不同,背后有技术。谢谢。
猜你喜欢
  • 2013-08-24
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
相关资源
最近更新 更多