views:

264

answers:

2

When i'm trying to put russian text in cookie via javascript and then output it via php it returns:

%u043F%u0440%u043E%u0432%u0435%u0440%u043A%u0430

How to decode this to normal cyrillic characters?

This is the function i'm using to pass to document.cookie:

function setCookie(c_name,val,c_expiredays,c_path,c_domain,c_secure)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+c_expiredays);
    document.cookie=c_name+ "=" +escape(val)+

    /* Additional settings */

((c_path) ? "; path=" + c_path : "") + ((c_domain) ? "; domain=" + c_domain : "") + // used to allow using only on a certain domain ((c_secure) ? "; secure" : "") + // used for HTTPS (SSL)

    ((c_expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

setCookie('name',$(this).val(),1);

On server side, i'm outputting like that:

(isset($_COOKIE['img_href_value']) ? $_COOKIE['img_href_value'] : '')
+1  A: 

You can't put non-ASCII characters directly in a cookie (what happens if you try varies over each different browser and many of them mangles the results irretrievably).

So you have to choose some encoding to use to use on cookie values. It doesn't matter what, as long as your client-side JavaScript and server-side PHP agree on one encoding. URL-encoding is certainly a popular choice of encoding scheme for this purpose, but it's not mandated by any standard and tools won't automatically decode it for you.

  • To get characters out of a URL-encoded cookie value you must manually call rawurldecode on the value at the PHP end, or decodeURIComponent to extract from document.cookie at the JavaScript end.

  • To encode to this format the corresponding functions are rawurlencode on the PHP side and encodeURIComponent in JavaScript.

  • (This assumes you are using UTF-8 for your strings, which you should be.)

Don't use urlencode for this in PHP (it's for form submission parameters only and gets the space character wrong in this context), and definitely don't use escape in JavaScript (it gets every non-ASCII character wrong, coming up with that weird non-standard %uNNNN format you quoted).

(In general, JS escape/unescape is an ancient and highly questionable encoding scheme that you should almost never have any reason to use.)

bobince