当GET和POST发送相同的值时,$_REQUEST将返回什么?


what $_REQUEST will return when same values sent by GET and POST?

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

在尝试这个时,我只是混淆了GET, POSTREQUEST

查看此情况

<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=",会发生什么?

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

通常默认设置是Get然后Post。在本例中,作为get和post参数提供id参数。这意味着先用$_GET填充$_REQUEST,然后用$_POST填充。这意味着$_REQUEST将反映$_POST。

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和$_POST和使用$_REQUEST[]有什么错?