方法=“发布” enctype=“文本/纯文本”不兼容?

发布于 2024-12-07 23:17:40 字数 195 浏览 0 评论 0原文

当我使用

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

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

When I use

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

form data can not be sent to proc.php file properly. Why? What is the problem? Why I can't use text/plain encoding with post but I can use it with get method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友谊不毕业 2024-12-14 23:17:40

[修订]

答案是,因为 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 可能出错的示例:

file1.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>

file2.php:

<?php
print($HTTP_RAW_POST_DATA);
?>

结果:

input1=abc
input2=def
input2=ghi

无法区分 input1的值是什么输入2变量。可以是

  • 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),您可以选择是否要将它们作为 text/plainapplication/x-www-form-urlencoded 发送,但第二个是唯一的明确的解决方案。

[Revised]

The answer is, because PHP doesn't handle it (and it is not a bug):

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

Valid values for enctype in <form> tag are:

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

The first is the default, the second one you need only when you upload files.

@Alohci provided explanation why PHP doesn't populate $_POST array, but store the value inside a variable $HTTP_RAW_POST_DATA.

Example of what can go wrong with text/plain enctype:

file1.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>

file2.php:

<?php
print($HTTP_RAW_POST_DATA);
?>

Result:

input1=abc
input2=def
input2=ghi

No way to distinguish what is the value of input1 and input2 variables. It can be

  • input1=abc\r\ninput2=def, input2=ghi, as well as
  • input1=abc, input2=def\r\ninput2=ghi

No such problem when using the other two encodings mentioned before.

The difference between GET and POST:

  • in GET, the variables are part of URL and are present in URL as query string, therefore they must be URL-encoded (and they are, even if you write enctype="text/plain" - it just gets ignored by the browser; you can test it using Wireshark to sniff the request packets),
  • when sending POST, the variables are not part of URL, but are sent as the last header in HTTP request (POSTDATA), and you can choose whether you want to send them as text/plain or application/x-www-form-urlencoded, but the second one is the only non-ambiguous solution.
偏闹i 2024-12-14 23:17:40

HTML5 确实定义了如何格式化以 text/plain 形式提交的表单数据:https://w3c.github.io/html/sec-forms.html#plain-text-form-data

在该部分的底部,它说:

使用文本/纯格式的有效负载是为了便于人类使用
可读。它们不能被计算机可靠地解释,因为
格式不明确(例如,无法区分
值末尾的换行符中的文字换行符)。

因此,PHP 不尝试解释它而仅以原始形式提供它,这并非没有道理。对我来说,这似乎是正确的方法。

HTML5 does define how to format form data submitted as text/plain here: https://w3c.github.io/html/sec-forms.html#plain-text-form-data.

At the bottom of that section, it says:

Payloads using the text/plain format are intended to be human
readable. They are not reliably interpretable by computer, as the
format is ambiguous (for example, there is no way to distinguish a
literal newline in a value from the newline at the end of the value).

So it not unreasonable that PHP does not attempt to interpret it and only makes it available in raw form. To me, that seems the correct approach.

不…忘初心 2024-12-14 23:17:40

如果您使用 enctype="text/plain" 进行调试,请记住 $_POST 仅包含通过 HTTP 传递到当前脚本的关联变量数组使用 application/x-www-form-urlencodedmultipart/form-data 作为 HTTP Content-Type/enctype 时的 POST 方法要求。因此,如果您在 php 脚本中打印 $_POST,您将无法在脚本中看到任何内容。

还有一个名为 $HTTP_RAW_POST_DATA 的全局变量,它在 PHP 5.6 中已弃用,并从 PHP 7.0 开始已删除。您可以使用它来查看原始表单数据,如N'Bayramberdiyev所述

if you're using enctype="text/plain" for debugging purposes, keep in mind that $_POST only contains associate array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type/enctype in the request. So you won't be able to see anything in the script if you're printing $_POSTin the php script.

Also there was a global variable called $HTTP_RAW_POST_DATA which is DEPRECATED in PHP 5.6, and REMOVED as of PHP 7.0. which you could have used to see raw form data as noted by N'Bayramberdiyev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文