According to the ancient Netscape cookie_spec:
This string is a sequence of characters excluding semi-colon, comma and white space.
By implication the =
character is also disallowed in the name part. So -
should work, and it does seem to be OK in browsers I've got here; where are you having trouble with it?
What that document doesn't remember to say, because Netscape were terrible at writing specs, was that control characters (\x00
to \x1F
plus \x7F
) aren't allowed, and support for non-ASCII characters is left unspecified.
What browsers do:
- in Opera and Google Chrome, non-ASCII characters are encoded into cookies with UTF-8;
- in IE, the machine's default code page is used (locale-specific and never UTF-8);
- Firefox (and other Mozilla-based browsers) use the low byte of each UTF-16 code point on its own (so ISO-8859-1 is OK but anything else is mangled);
- Safari simply refuses to send any cookie containing non-ASCII characters.
so in practice you cannot use non-ASCII characters in cookies at all. If you want to use Unicode, control codes or other arbitrary byte sequences you must use an ad-hoc encoding scheme of your own choosing. Most popular is UTF-8-inside-URL-encoding (as produced by JavaScript's encodeURIComponent
).
There is another, proper internet standard for Cookies: RFC2965. In this standard many more special characters are disallowed, as it uses RFC2616 tokens (a -
is still allowed there), and only the value may be specified in a quoted-string with other characters.
However you should ignore this spec because no browser implements anything in it. In the real world we are still using the original-and-worst Netscape cookie_spec.