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

发布于 2024-11-29 23:27:14 字数 457 浏览 0 评论 0 原文

我有一个通过 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>

I have a form that submits via GET, and one of the hidden fields submits a list of category IDs, separated by comma (1,2,3).

When the get query gets to the page it is going, commas become escaped with %2C.

I cannot make changes to PHP that parses these values, and they must remain commas.

In summary: ?category=1,2,3 works, and ?category=1%2C2%2C3 doesn't.

How do I prevent the comma from being encoded?

Edit to address the comment, simplified, but gives you the gist:

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

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

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

发布评论

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

评论(4

月下客 2024-12-06 23:27:14

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

2.2。保留字符

许多 URI 包含由某些元素组成或分隔的组件
特殊字符。这些字符被称为“保留”,因为
它们在 URI 组件中的使用仅限于它们的保留
目的。如果 URI 组件的数据与
保留目的,那么冲突的数据必须在之前转义
形成 URI。

<前><代码>保留=“;” | “/” | “?” | “:”| “@” | “&” | “=” | “+”|
“$” | “,”

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

您的解决方案在于

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

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

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

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

The problem with "making it stop" is that the encoding is a part of HTTP standards - you "shouldn't want" to make it stop since it is a part of the very basis upon which HTTP is built. RFC2396 describes which characters are allowed and not allowed in a 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    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                "
quot; | ","

Because of this fact, when using GET to submit a form, the user agent will encode the values according to this specification.

Your solution lies in either

  1. Change the form to use the POST method, change references to $_GET into $_POST in php

  2. Call urldecode (docs) on the data before using it ($_GET['my_value'] = urldecode($_GET['my_value']);)

  3. Use element arrays to submit this as an array to the server

On PHP side, $_GET['myElement'] will be equal to array(1,2,3)

幸福丶如此 2024-12-06 23:27:14

使用Javascript手动编码查询字符串?虽然有点丑,但看起来这是唯一的选择。

Use Javascript to manually encode the query string? A bit ugly, but it looks like it is the only option.

甲如呢乙后呢 2024-12-06 23:27:14

创建 3 个具有相同名称“类别”和不同值 1、2 和 3 的隐藏字段。

Create 3 hidden fields with the same name "category" and a different value 1, 2 and 3.

怕倦 2024-12-06 23:27:14

不要阻止编码,而是考虑在收到字符串时对其进行解码。这是一个示例(使用 java):

public class Encoden
{
    public static void main(String[] args)
    {
        String encodedValue;
        String value = "a, b, c";
        String unencodedValue;

        try
        {
            encodedValue = URLEncoder.encode(value, "UTF-8");
        }
        catch (UnsupportedEncodingException exception)
        {
            encodedValue = null;

            System.out.print("encoding exception: ");
            System.out.println(exception.getMessage());
        }

        try
        {
            unencodedValue = URLDecoder.decode(encodedValue, "UTF-8");
        }
        catch (UnsupportedEncodingException exception)
        {
            unencodedValue = null;
            System.out.print("decoding exception: ");
            System.out.println(exception.getMessage());
        }

        System.out.print("Original: ");
        System.out.println(value);
        System.out.print("Encoded: ");
        System.out.println(encodedValue);
        System.out.print("Decoded: ");
        System.out.println(unencodedValue);
    }
}

我刚刚注意到 php 标签。虽然我不懂 php,但我确信它有一种方法来编码和解码 HTML 字符串值。

编辑:
根据注释,尝试渲染 CDATA 块内隐藏的值。我不知道这是否有效,只是把它扔在那里。下面是一个示例:

Instead of preventing encoding, consider decoding the string when you receive it. Here is an example (using java):

public class Encoden
{
    public static void main(String[] args)
    {
        String encodedValue;
        String value = "a, b, c";
        String unencodedValue;

        try
        {
            encodedValue = URLEncoder.encode(value, "UTF-8");
        }
        catch (UnsupportedEncodingException exception)
        {
            encodedValue = null;

            System.out.print("encoding exception: ");
            System.out.println(exception.getMessage());
        }

        try
        {
            unencodedValue = URLDecoder.decode(encodedValue, "UTF-8");
        }
        catch (UnsupportedEncodingException exception)
        {
            unencodedValue = null;
            System.out.print("decoding exception: ");
            System.out.println(exception.getMessage());
        }

        System.out.print("Original: ");
        System.out.println(value);
        System.out.print("Encoded: ");
        System.out.println(encodedValue);
        System.out.print("Decoded: ");
        System.out.println(unencodedValue);
    }
}

I just noticed the php tag. While I dont know php, I'm certain that it will have a means to encode and decode HTML string values.

Edit:
Based on comments, try rendering the value of the hidden inside a CDATA block. I have no idea if this will work, just throwing it out there. Here is an example:

<input type="hidden" name="blam" value="<![CDATA[1, 2, 3]]>"/>

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