php - 从 PHP 读取时更改 Cookie 信息

标签 php javascript cookies

我的问题是这样的。我正在使用 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/

相关文章:

java - Spring Security 记住我注销问题

php - 在 yii2 Activerecord 的关系表中获取计数

javascript - 如何在原型(prototype)函数中访问javascript对象变量

asp.net - 攻击aspx网站的方法

javascript - 交通灯类(class) - A452

javascript - 为什么 Chrome 让我在这个循环中声明我的变量?

ruby-on-rails-3 - 使用 Poltergeist PhantomJS Capybara 驱动程序设置 Cookie

php - 如何使用 PHP 将 base64 编码的二进制数据保存为 zip

php - 使用 JavaScript 仅允许文本框值介于 1 到 11 之间

php - 如何同时向两个表插入数据?