Client cookie Chinese programming

zhaozj2021-02-08  286

Client Cookie Chinese Programming 2000-11-28 · · Yesky

-------------------------------------------------- -----------------------------

In the use of cookie, we find such a problem: if the content written to cookie is Chinese (such as user name), use the server-side program (such as ASP or PHP) to read completely correct, but read with general JavaScript or VBScript The cookie function taken out is a pile of garbled. This is a tricky problem, because in some cases, the content of the cookie needs to be read in the client side with scripting language. If you write Chinese, get a bunch of garbled, is it very awkward? Solve this issue, you have to start from the cookie's access. We know that you need Escape (hexadecimal code) before writing to cookie, and the encoding is in bytes. This is the key to the problem: Any Chinese character will be dismantled into two bytes. At the time of reading cookies, UNESCAPE decodes in byte units, so the last Chinese characters become garbled for two bytes. How to do it? The ASP or PHP mode can be read correctly, and it should be the unicode encoding in the UNICODE encoding in the UNESCAPE. If this is, can I find a way to solve this problem? After careful research, it was found that as long as the Chinese character information can be saved during the hex Codec (codec) process, we need to have an additional CODEC process, the program is as follows:

1. Before the ESCAPE encoding, convert each character in the cookie string into a string form of the Unicode code (with a special character as a partition of the string).

2. After UNESCAPE decoding, first extract all Unicode strings, and then convert to the original character with the corresponding function.