How to read Chinese cookies using Javascript

Source: Internet
Author: User

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 issue, because in some cases, the Cookie content needs to be read from the client using the script language. If you write Chinese characters and get a bunch of garbled characters, isn't it 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: every Chinese character is split into two bytes for encoding. While reading cookies, unescape decodes each character in bytes. Therefore, every Chinese character is garbled into two bytes. What should we do? The ASP or PHP method can be correctly read. It should be after the unescape decoding, and then the Chinese characters are spelled out according to the unicode encoding. If so, can we find a work und to solve this problem? After careful research, we found that as long as the Chinese character information can be saved in the hexadecimal codec (codec) process, we need to outsource another codec process. The solution is as follows:

1. Before escape encoding, convert each character in the Cookie string to a unicode character string (using a special character as the separator of the string ).

2. After decoding unescape, extract all unicode strings and convert them to the original character using the corresponding function.

Copy codeThe Code is as follows:
<Script language = javascript>
<! --
// Encoding program:
Function CodeCookie (str)
{
Var strRtn = "";

For (var I = str. length-1; I> = 0; I --)
{
StrRtn + = str. charCodeAt (I );
If (I) strRtn + = "a"; // use a as the Separator
}
Return strRtn;
}

// Decoder:
Function DecodeCookie (str)
{
Var strArr;
Var strRtn = "";

StrArr = str. split ("");

For (var I = strArr. length-1; I> = 0; I --)
StrRtn + = String. fromCharCode (eval (strArr [I]);

Return strRtn;
}
// -->
</Script>


The VBScript version is as follows:

Copy codeThe Code is as follows:
<Script language = vbscript>
<! --
"Encoding program:
Function CodeCookie (str)
Dim I
Dim strRtn

For I = len (str) to 1 step-1
StrRtn = strRtn & ascw (mid (str, I, 1 ))
If (I <> 1) then strRtn = strRtn & "a" "uses a as the Separator
Next
CodeCookie = strRtn
End function

"Decoder:
Function DecodeCookie (str)
Dim I
Dim strArr, strRtn

StrArr = Split (str, "");

For I = UBound (strArr)-LBound (strArr) to 1 step-1
StrRtn = strRtn & chrw (Val (strArr (I )))
Next

DecodeCookie = strRtn
End function
-->
</Script>

For example, if the Cookie content you want to write is "abc", after CodeCookie is changed to "99a98a97a-31029a000032a" (reverse the string based on security considerations ), after escape encoding, it becomes "99a98a97a % 2d31029a000032a" (escape encoding converts characters other than letters and numbers to hexadecimal % XX format). Note that the separator cannot be set to %, D and number. Of course, if you have important information similar to a password, you need to encrypt it. Because Cookie writing is generally short messages, some bytes added after encoding are negligible. The following javascript Cookie reading and writing functions include the above Chinese support.

Function SetCookie (name, value, expires)
{
Var exp = new Date ();
Exp. setTime (exp. getTime () + expires * 60*1000 );
Documents. cookie = name + "=" + escape (CodeCookie (value) + "; expires =" + exp. toGMTString () + "; path = /";
}

Function GetCookie (name)
{
Var strArg = name + "= ";
Var nArgLen = strArg. length;
Var nCookieLen = documents. cookie. length;
Var nEnd;
Var I = 0;
Var j;

While (I <nCookieLen)
{
J = I + nArgLen;
If (documents. cookie. substring (I, j) = strArg)
{
NEnd = documents. cookie. indexOf (";", j );
If (nEnd =-1) nEnd = documents. cookie. length;
Return DecodeCookie (unescape (events. cookie. substring (j, nEnd )));
}
I = documents. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}


If the CGI program is used to write a Chinese Cookie, the client reads the Cookie. For example, in ASP, you can use the preceding encoding function and then write it with response, for example: response. cookie ("Name") = CodeCookie ("Michael") now, the problem of Chinese cookies is basically solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.