2

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.

Sami Al-Subhi
  • 3,594
  • 8
  • 34
  • 54

1 Answers1

1

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.

Community
  • 1
  • 1
Starx
  • 75,098
  • 44
  • 181
  • 258
  • http://stackoverflow.com/questions/2820249/how-do-i-base64-encode-and-decode-using-javascript – Sami Al-Subhi Apr 04 '12 at 13:23
  • @SamiAl-Subhi, Thats a php function. Your question is tagged, PHP ain't it? – Starx Apr 04 '12 at 13:24
  • @SamiAl-Subhi, Check out [this](http://stackoverflow.com/q/246801/295264) question too. – Starx Apr 04 '12 at 13:26
  • I use javascript to write and php to read. therefore this feature should exist in both. I checked ur link and doesnt say any thing about IE. isnt there something has do with utf-8 like setting javascript to write in utf-8. – Sami Al-Subhi Apr 04 '12 at 13:32
  • btoa and atob only work properly for ASCII based strings. therefore I cant use it here. – Sami Al-Subhi Apr 04 '12 at 13:33