2

I am trying to work between PHP and Javascript, reading and writing the same cookie that contains a json value that I read into an array in both JS and PHP.

If I save the cookie having used json_encode() on my php array I find that the contents of the resulting cookie are all encoded, as if url encoded.

The problem is that when I try to then read that cookie and decode the JSON with

var existing_array = readCookie('cookie_name');
JSON.parse(existing_array)

I get a javascript error (such as Uncaught SyntaxError: Unexpected token %)

If I use a browser based cookie reader I can see that the array is easily readable when it is saved via javascript: ["644","636"].

However when the array is created in PHP via the json_encode() function the resulting content ends up as something like: %5B%22644%22%5D.

If this encoding wasn't happening i'm confident that the JS function could decode it. Has anyone else experienced this? I'm using PHP 5.5 on Windows 8 and the latest Chrome Browser.

2
  • 2
    Can't you just decode in JS? decodeURI( readCookie('...') ) Commented Apr 18, 2015 at 17:57
  • The contents in the cookie is URL encoded. Decode it first if you want to JSON.parse it properly. Commented Apr 18, 2015 at 17:58

2 Answers 2

4

Cookies need to be stuffed in HTTP headers so they get encoded for transmision:

setcookie('cookie_name', '["644","636"]');

... triggers:

Set-Cookie: cookie_name=%5B%22644%22%2C%22636%22%5D

... and the browser sends this back:

Cookie: cookie_name=%5B%22644%22%2C%22636%22%5D

Your readCookie() function should take care of decoding data properly but it seems it doesn't. If you want to fix it, you can take some ideas from e.g. the jquery-cookie plug-in:

function parseCookieValue(s) {
    if (s.indexOf('"') === 0) {
        // This is a quoted cookie as according to RFC2068, unescape...
        s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
    }

    try {
        // Replace server-side written pluses with spaces.
        // If we can't decode the cookie, ignore it, it's unusable.
        // If we can't parse the cookie, ignore it, it's unusable.
        s = decodeURIComponent(s.replace(pluses, ' '));
        return config.json ? JSON.parse(s) : s;
    } catch(e) {}
}

Please note the decodeURIComponent part.

Sign up to request clarification or add additional context in comments.

Comments

4

Thank you @Evilzebra, it was simply adding decodeURIComponent at the start of the JS that resolved it.

decodeURIComponent(readCookie('cookie_name'));

2 Comments

Looks like a messy solution. Why use a readCookie() function that can't read a cookie properly?
JSON.parse($.cookie(cookie_name')) seems like a better solution, reference stackoverflow.com/questions/18318545/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.