gpt4 book ai didi

php - 为什么 PHP 将 $_COOKIE 中的加号替换为空格?

转载 作者:IT王子 更新时间:2023-10-29 00:03:58 25 4
gpt4 key购买 nike

所以根据我对 PHP 和 cookie 的理解,如果我使用 setcookie() 函数,那么我会得到一个自动 url 编码的 cookie。当我转到 $_COOKIE 数组时,我应该取回 cookie,自动进行 url 解码。问题是,当我查看 $_COOKIE 时,它似乎对 cookie 进行了两次解码。

假设我有一个值为“Name|ID|Email”的 cookie,例如:

Joe|123|my+email@somewhere.com

这将被编码为:

Joe%7C123%7Cmy%2Bemail%40somewhere.com

请注意,加号是经过编码的,因此理论上,如果我对其进行解码,我应该将其取回。因为这是在 $_COOKIE 中自动完成的,所以我应该恢复原来的状态。但相反,我回来了:

Joe|123|my email@somewhere.com

注意加号所在的空间。如果我在 cookie 上运行额外的 urldecode(),这就是我所期望的。但我不是,所以我不知道为什么我会得到一个空间而不是加号。

另一个有趣的转折。页面刷新似乎产生了正确的输出。知道为什么它会这样吗?

仅供引用,为了设置初始 cookie,我使用 javascript 和 escape() 脚本来生成编码字符串。这可能是 javascript 和 PHP 之间的交接问题吗?

想法将不胜感激。

最佳答案

值得注意的是,“%20”和“+”都是空格字符的有效编码。根据关于 URL encoding 的维基百科文章(强调):

When data that has been entered into HTML forms is submitted, the form field names and values are encoded and sent to the server in an HTTP request message using method GET or POST, or, historically, via email. The encoding used by default is based on a very early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with "+" instead of "%20". The MIME type of data encoded this way is application/x-www-form-urlencoded, and it is currently defined (still in a very outdated manner) in the HTML and XForms specifications.

更具体地与 PHP 和 JavaScript 相关,请参阅此问题的最佳答案:

When to encode space to plus (+) or %20?

关于php - 为什么 PHP 将 $_COOKIE 中的加号替换为空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com