为什么PHP将$ _COOKIE中的空格替换为空格? [英] Why does PHP replace pluses with spaces in $_COOKIE?

查看:571
本文介绍了为什么PHP将$ _COOKIE中的空格替换为空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以从我对PHP和cookies的理解,如果我使用 setcookie()函数,那么我得到一个自动url编码的cookie。当我去到 $ _ COOKIE 数组,我应该得到cookie回来,自动url解码。问题是,当我在 $ _ COOKIE 中查找时,它似乎会解码两次。

So from my understanding of PHP and cookies, if I use the setcookie() function, then I get a cookie that is automatically url encoded. And when I go to the $_COOKIE array, I should get the cookie back, automatically url decoded. Problem is, it seems to be decoding the cookie twice when I look in $_COOKIE.

值为Name | ID | Email的Cookie,例如:

Say I have a cookie whose value is "Name|ID|Email", for example:


Joe|123|my+email@somewhere.com


Joe|123|my+email@somewhere.com

这将编码为:


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

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

请注意加号是编码的,所以理论上我应该得到它,如果我解码。因为这是在 $ _ COOKIE 中自动完成的,我应该得到我开始的。但我会回来:

Notice the plus sign is encoded, so theoretically I ought to get it back if I decode it. Since this is automatically done in $_COOKIE, I ought to get back what I started with. But instead, I'm getting back:


Joe | 123 | my email@somewhere.com

Joe|123|my email@somewhere.com

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

Notice the space where the plus used to be. This is what I would expect if I ran an additional urldecode() on the cookie. But I'm not, so I have no idea why I would be getting a space instead of a plus.

另一个有趣的转折。刷新页面似乎产生正确的输出。

Another interesting twist. A refresh on the page seems to produce the correct output. Any ideas why it's behaving like this?

FYI,为了设置初始cookie,我使用javascript和 escape()脚本生成编码字符串。这可能是javascript和PHP之间的一个切换问题?

FYI, to set the initial cookie, I use javascript and escape() the script to produce the encoded string. Might this be an hand off issue between javascript and PHP?

想法会很感激。

推荐答案

值得注意的是,%20和+都是空格字符的有效编码。根据网址编码的维基百科文章(增加了强调):

It's worth noting that both "%20" and "+" are valid encodings of a space character. Per the Wikipedia article on URL encoding (emphasis added):


当提交输入到HTML表单中的数据时,
字段名称和值的形式被编码,并在HTTP
请求消息中使用GET或GET方法发送到服务器。 POST,或者,历史上,通过电子邮件。
默认使用的编码基于
一般URI百分比编码规则的非常早期版本,具有许多修改
,例如换行符归一化,并将空格替换为 +,而不是
%20
。以这种方式编码的数据的MIME类型是
application / x-www-form-urlencoded,它目前在HTML和XForms规范中定义(仍然是
,非常过时)。

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相关,请参阅这个问题的最高答案:

More specifically related to PHP and JavaScript, see the top answer on this question:

何时将空格编码为加号(+)和何时到%20?

这篇关于为什么PHP将$ _COOKIE中的空格替换为空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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