In the use of cookies, we found a problem: if the content written into the Cookie is in Chinese (such as the user name), the server-side program (such as ASP or PHP) should read it completely correctly, however, a bunch of garbled characters are extracted using the common JavaScript or VBScript's read Cookie function. This is a tricky problem, because in some cases, the Cookie content needs
In the use of cookies, we found a problem: if the content of the Cookie written is in Chinese (such as the user name), use the server program (such as ASP
Or PHP), but a bunch of garbled characters are obtained using the general JavaScript or VBScript read Cookie function. This is a tough issue,
In some cases, the Cookie content needs to be read in the script language on the client side. If you write Chinese characters and get a bunch of garbled characters, doesn't it feel so bad?
Awkward? To solve this problem, we should also start with the Cookie access method.
We know that escape (hexadecimal encoding) is required before the Cookie content is written, and the encoding is in bytes. this is the key to the problem.
In: any Chinese character is split into two bytes for encoding, while in the Cookie reading, unescape decodes the characters in bytes. Therefore, each Chinese character is changed.
Into two byte garbled characters. What should we do? The ASP or PHP method can be correctly read. it should be after the unescape decoding, and then according to the unicode encoding
Words are spelled out. If so, can we find a work und to solve this problem? After careful research, we found that as long as the hexadecimal codec
It is enough to store Chinese characters in the process. Therefore, we need to outsource a codec process. The solution is as follows:
1. before escape encoding, convert each character in the Cookie string to a unicode character string format (separated by a special character as a string ).
).
2. after decoding unescape, extract all unicode strings and convert them to the original character using the corresponding function.