php - 不要在表单提交时转义特殊字符

标签 php html forms escaping

我有一个通过 GET 提交的表单,其中一个隐藏字段提交了类别 ID 列表,以逗号 (1,2,3) 分隔。

当 get 查询到达它要访问的页面时,逗号将转义为 %2C

我无法更改解析这些值的 PHP,它们必须保留为逗号。

总结:?category=1,2,3 有效,而 ?category=1%2C2%2C3 无效。

如何防止逗号被编码?

编辑以解决评论,简化,但为您提供要点:

<form method="get" action="something.php">
<input type="hidden" name="category" value="1,2,3">
<input type="submit">
</form>

最佳答案

“让它停止”的问题是编码是 HTTP 标准的一部分——你“不应该”让它停止,因为它是构建 HTTP 的基础的一部分。 RFC2396描述 URI 中允许和不允许使用的字符:

2.2. Reserved Characters

Many URI include components consisting of or delimited by, certain special characters. These characters are called "reserved", since
their usage within the URI component is limited to their reserved
purpose. If the data for a URI component would conflict with the
reserved purpose, then the conflicting data must be escaped before
forming the URI.

  reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                "$" | ","

因此,当使用 GET 提交表单时,用户代理将根据此规范对值进行编码。

你的解决方案在于要么

  1. 更改表单以使用 POST 方法,将对 $_GET 的引用更改为 php 中的 $_POST

  2. 在使用数据之前调用 urldecode ( docs ) ($_GET['my_value'] = urldecode($_GET['my_value']);)

  3. 使用元素数组将其作为数组提交给服务器

在 PHP 端,$_GET['myElement'] 将等于 array(1,2,3)

关于php - 不要在表单提交时转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095474/

相关文章:

phpmailer 与 mysql 结果

html - 在 HTML5 中标记 "BBC pattern"

html - 如何居中使用 BootstrapValidate 创建的错误消息?

css - 将字段集元素定位在其他元素下

如果 Django 表单缺少字段,则 JavaScript 警报框

php - 带逗号的单独 PHP 数组的数据

javascript - 窗口内弹出

php - 为什么我的 session 比我设置的时间更快到期?

php - Bootstrap 下拉导航在包含在 php 文件中时不起作用

jquery - Div容器不以边距自动居中