我的问题是这样的。我正在使用 java 脚本设置一个 cookie,其中包含值
"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg"
现在,当我在 PHP 页面上接收并读取这个 cookie 时,我得到的内容是
"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK Hntg"
'+' 符号更改为空格字符。为什么会这样?
最佳答案
根据 document.cookie引用:
The cookie value string can use encodeURIComponent() to ensure that the string does not contain any commas, semicolons, or whitespace (which are disallowed in cookie values).
所以这就是你需要做的:
document.cookie = "foobar=" + encodeURIComponent("MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg");
alert(document.cookie); // + becomes %2B which PHP will interpret and decode automatically
关于php - 从 PHP 读取时更改 Cookie 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7912742/