从PHP读取时更改Cookie信息

时间:2011-10-27 07:15:31

标签: php javascript cookies

我的问题是这个。我正在使用包含值

的java脚本设置co​​okie
"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg"  

现在,当我在PHP页面上接收并阅读此cookie时,我将内容视为

"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK Hntg"

'+'符号更改为空格字符。为什么会这样?

1 个答案:

答案 0 :(得分:1)

根据document.cookie参考:

  

cookie值字符串可以使用encodeURIComponent()来确保   该字符串不包含任何逗号,分号或空格   (在cookie值中不允许使用)。

所以这就是你需要做的事情:

document.cookie = "foobar=" + encodeURIComponent("MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg");
alert(document.cookie); // + becomes %2B which PHP will interpret and decode automatically
相关问题