方法=“发布” enctype=“文本/纯文本”不兼容?
当我使用
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[修订]
答案是,因为 PHP 不处理它(并且它不是一个错误):
https://bugs.php.net/bug.php?id=33741
第一个是默认值,第二个仅在上传文件时需要。
@Alohci 解释了为什么 PHP 不填充
$_POST
数组,而是将值存储在变量$HTTP_RAW_POST_DATA
中。text/plain
enctype 可能出错的示例:file1.php:
file2.php:
结果:
无法区分
input1
和的值是什么输入2变量。可以是
abc\r\ninput2=def
、input2=ghi
,以及abc
、input2=def\r\ninput2=ghi
使用前面提到的其他两种编码时没有这样的问题。
GET 和 POST 之间的区别:
enctype="text/plain"
- 它只是被浏览器忽略;您可以使用 Wireshark 嗅探请求数据包来测试它),text/plain
或application/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
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:
file2.php:
Result:
No way to distinguish what is the value of
input1
andinput2
variables. It can beabc\r\ninput2=def
, input2=ghi
, as well asabc
, input2=def\r\ninput2=ghi
No such problem when using the other two encodings mentioned before.
The difference between GET and POST:
enctype="text/plain"
- it just gets ignored by the browser; you can test it using Wireshark to sniff the request packets),text/plain
orapplication/x-www-form-urlencoded
, but the second one is the only non-ambiguous solution.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:
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.
如果您使用
enctype="text/plain"
进行调试,请记住$_POST
仅包含通过 HTTP 传递到当前脚本的关联变量数组使用application/x-www-form-urlencoded
或multipart/form-data
作为 HTTPContent-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 usingapplication/x-www-form-urlencoded
ormultipart/form-data
as the HTTPContent-Type/enctype
in the request. So you won't be able to see anything in the script if you're printing$_POST
in 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