April 4, 2012

How to write a non-english character cookie using javascript and let php reads it properly

Question by Sami Al-Subhi

my websites sometimes needs to read a javascript cookie using php but sometimes I get weird character set from some users like this `#16 3CFJ) DD%J,’1 while for some users it reads it properly. therefore, I think the problem is in the client-side. I use this method to write cookies:

    var expireDate = new Date();
        expireDate.setMonth(expireDate.getMonth() + 1);
    var value="Sami";
    document.cookie = "name="+value+";path=/;expires="+expireDate.toGMTString();

and this $_COOKIE['name']to read it using php.

Answer by Starx

Cookies cant be handled using headers. So,

Encode your cookie using base64_encode() and decode it using base64_decode() to read it.

To encode/decode in Javascript, this answer might help.

...

Please fill the form - I will response as fast as I can!