Andrew said:
How does the length of a string relate to the 4,096 bytes allowable in a
cookie. Will, for example, a string of 100 characters equal 100 bytes in
a cookie?
It equals to 100 only if all characters in the string are below code
point 256. These are the following:
http://en.wikipedia.org/wiki/File:ASCII_full.svg
Wikipedia tells me that UTF-8 encodes each character in 1 to 4 octets
(8-bit bytes). Doesn't that mean a string 100 characters could be as
large as 400 bytes?
Yes, UTF-8 characters have a variable byte length (min. 1, max. 4).
But that doesn't matter here. A cookie goes into the HTTP-header and
may only contain ASCII characters, so the string *must* be encoded
into ASCII before it can be sent as a cookie. This should preferably
be done with an UTF-8 percent-encoding; see 'encodeURI()' in
javascript. Afterwards you could check whether the encoded result is
still shorter than 4096 bytes.
var str = 'café \u1234';
var enc_str = encodeURI(str);
var report = 'Original string: ' + str + '\n'
+ 'Original string length: ' + str.length + '\n'
+ 'Encoded string: ' + enc_str + '\n'
+ 'Encoded string length: ' + enc_str.length;
alert(report);
if (enc_str.length > 4096) {
alert('cookie too long')
}
else {
alert('cookie not too long');
}
'decodeURI()' is the opposite of 'encodeURI()' and can be used to
retrieve the original value from the cookie. In theory, you could also
use HTML num/char entities; or anything you like, as long as both
parties en/decode in the same manner AND as long as the cookie-string
is ASCII.
But percent-encoding would be the recommended way.
Hope this helps,