Russian characters in a cookie

V

Victor

Hi everybody,

could anybody help me with the following problem :

I need to set a cookie containing a Russian character string as the
value, using the construct "document.cookie = ...". The whole project
runs in the UTF-8 encoding.
The Internet Explorer handles this cookie correctly, whereas the
FireFox writes only some byte gargabe and then reads it back.

How can I solve it?

Regards

Victor
 
M

Martin Honnen

Victor wrote:

could anybody help me with the following problem :

I need to set a cookie containing a Russian character string as the
value, using the construct "document.cookie = ...". The whole project
runs in the UTF-8 encoding.
The Internet Explorer handles this cookie correctly, whereas the
FireFox writes only some byte gargabe and then reads it back.

Nowadays (since JScript 4 and JavaScript 1.3) strings in JavaScript are
sequences of Unicode characters so a Russian (cyrillic?) character is
just another Unicode character in a JavaScript string. In terms of
string handling a Russian character should not pose any problems.

As for document.cookie, how exactly do you write a value in there, how
do you read it back? Where do you notice "byte garbage"? Do you use the
escape function to escape the cookie value before setting
document.cookie, do you use the unescape function on the cookie value
you get from document.cookie?
 
V

Victor

First I have this function :

function setCookie (Bezeichner, Wert, Verfall) {
var jetzt = new Date();
var Auszeit = new Date(jetzt.getTime() + Verfall);
document.cookie = Bezeichner + "=" + Wert + "; expires=" +
Auszeit.toGMTString() + ";";
}

then I call it like this:

setCookie('Wert',someValue,123456789);

where someValue consists of Russian characters.

If I look into this cookie using FireFox Display Cookie Tool, there are
already only some badly identifiable characters inside. I could live
with it, as long as I would receive a proper content back, but this is
not the case here...

I evaluate this cookie in a PHP script on the server side thru
$HTTP_COOKIE_VARS, setting a variabe $wert, which gets printed in the
HTML-output. It read just the same what I had seen thru the FF Cookie
Tool before...

Strange enough, I have this problem only in the FF; the Internet
Explorer copes with it easily..

Victor
 
M

Martin Honnen

Victor said:
First I have this function :

function setCookie (Bezeichner, Wert, Verfall) {
var jetzt = new Date();
var Auszeit = new Date(jetzt.getTime() + Verfall);
document.cookie = Bezeichner + "=" + Wert + "; expires=" +
Auszeit.toGMTString() + ";";

Try whether
document.cookie = Bezeichner + "=" + encodeURIComponent(Wert) +
";expires=" + Auszeit.toGMTString() + ";";

improves things. If anything is UTF-8 encoded then PHP should deal with
that fine and url decode/unescape the value as needed when you access
$_COOKIE.

The cookie ends up as a HTTP header so without escaping stuff you should
run into problems.
 
V

Victor

It seems that the encodeURIComponent() has improved the things, indeed.
Anyway, my cookie works now both in the IE and the FF.

Unfortunately, I did not understand how do you mean it with the
decode/unescape in the PHP...

Victor
 
M

Martin Honnen

Victor said:
It seems that the encodeURIComponent() has improved the things, indeed.

Unfortunately, I did not understand how do you mean it with the
decode/unescape in the PHP...

I was trying to suggest that PHP will correctly decode the cookie value
(that JavaScript encoded) automatically when you read out cookie values
from $_COOKIE (or $HTTP_COOKIE_VARS).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top