By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

Russian characters in a cookie

P: n/a
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

Oct 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a


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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 10 '06 #2

P: n/a
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

Martin Honnen wrote:
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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 10 '06 #3

P: n/a


Victor wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 10 '06 #4

P: n/a
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

Oct 10 '06 #5

P: n/a


Victor wrote:
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).


--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 10 '06 #6

P: n/a
Oh, yeah!

Thank you Martin!

Victor

Oct 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.