方法=“发布"enctype=“文本/纯文本"不兼容? [英] method="post" enctype="text/plain" are not compatible?

查看:28
本文介绍了方法=“发布"enctype=“文本/纯文本"不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用

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

解决方案

[Revised]

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

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

 中 enctype 的有效值标签是:应用程序/x-www-form-urlencoded多部分/表单数据

第一个是默认的,第二个是你上传文件时才需要的.

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

text/plain 编码可能出错的例子:

file1.php:

<textarea name="input1">abcinput2=def<input name="input2" value="ghi"/><输入类型=提交"></表单>

file2.php:

<?php打印($HTTP_RAW_POST_DATA);?>

结果:

input1=abc输入 2=定义输入2=ghi

无法区分input1input2 变量的值是多少.可以

  • input1=abc input2=def, input2=ghi,以及
  • input1=abc, input2=def input2=ghi

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

GET 和 POST 的区别:

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

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?

解决方案

[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 input2=def, input2=ghi, as well as
  • input1=abc, input2=def input2=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.

这篇关于方法=“发布"enctype=“文本/纯文本"不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆