In the use of cookies, we found such a problem: if the content written in the cookie is in Chinese (such as the user's name), it is completely correct to read it with a server-side program (such as ASP or PHP), but it is completely correct to read it with general javascript or VBScript. What the Cookie function takes out is a bunch of gibberish. This is a tricky problem because in some cases the contents of the cookie need to be read using a scripting language on the client side. If you write Chinese and get a bunch of garbled characters, does it feel awkward? To solve this problem, we have to start with the cookie access method.
We know that escape (hexadecimal encoding) is required before writing the cookie content, and the encoding is in bytes. This is the crux of the problem: any Chinese character will be split into two characters. Each section is encoded separately; when reading the cookie, unescape decodes it in byte units, so in the end each Chinese character becomes two bytes of garbled code. What to do? If the ASP or PHP method can be read correctly, it should be that after unescape decoding, the Chinese characters are spelled out according to the unicode encoding. If this is the case, can you find a workaround to solve this problem? After careful study, we found that as long as the Chinese character information can be saved during the hexadecimal codec (encoding and decoding) process, we need to outsource another layer of codec process. The plan is as follows:
1. Before escape encoding , convert each character in the Cookie string into the string form of Unicode code (using a special character as the string separator).
2. After unescape decoding, first extract all unicode strings, and then use the corresponding function to convert them into original characters.