简体   繁体   中英

Change in Cookie information, when read from PHP

My problem is this. I'm setting a cookie using java script, which contains value

"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg"  

Now, when i receive and read this cookie on a PHP page, im getting the content as

"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK Hntg"

The '+' sign is change to a space character. Why is it so?

According to document.cookie reference:

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).

So this is what you need to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM