gpt4 book ai didi

php - 方法 ="post"enctype ="text/plain"不兼容?

转载 作者:技术小花猫 更新时间:2023-10-29 12:56:51 27 4
gpt4 key购买 nike

当我使用

<form method="post" enctype="text/plain" action="proc.php"> 

表单数据无法正确发送到 proc.php 文件。为什么?问题是什么?为什么我不能将 text/plain 编码用于 post 但我可以将它用于 get 方法?

最佳答案

[已修订]

答案是,因为 PHP 不处理它(这不是错误):

https://bugs.php.net/bug.php?id=33741

Valid values for enctype in <form> tag are:

application/x-www-form-urlencoded
multipart/form-data

第一个是默认的,第二个只有在上传文件时才需要。

@Alohci 解释了为什么 PHP 不填充 $_POST 数组,而是将值存储在变量 $HTTP_RAW_POST_DATA 中。

text/plain enctype 可能出错的示例:

文件 1.php:

<form method="post" enctype="text/plain" action="file2.php">
<textarea name="input1">abc
input2=def</textarea>
<input name="input2" value="ghi" />
<input type="submit">
</form>

文件2.php:

<?php
print($HTTP_RAW_POST_DATA);
?>

结果:

input1=abc
input2=def
input2=ghi

无法区分input1input2 变量的值。可以是

  • input1=abc\r\ninput2=def, input2=ghi, 以及
  • input1=abc, input2=def\r\ninput2=ghi

使用前面提到的其他两种编码时没有这个问题。

GET 和 POST 的区别:

  • 在 GET 中,变量是 URL 的一部分并作为查询字符串出现在 URL 中,因此它们必须是 URL 编码的(而且它们是,即使您编写 enctype="text/plain" - 它只是被浏览器忽略;您可以使用 Wireshark 来测试它以嗅探请求数据包),
  • 发送 POST 时,变量不是 URL 的一部分,而是作为 HTTP 请求 (POSTDATA) 的最后一个 header 发送,您可以选择是否以 text/plain 发送或 application/x-www-form-urlencoded,但第二个是唯一明确的解决方案。

关于php - 方法 ="post"enctype ="text/plain"不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7628249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com