In the use of cookies, we found a problem: if the contents of a cookie are written in Chinese (such as a user's salutation), read it completely correctly with a server-side program (such as ASP or PHP). But using a normal JavaScript or VBScript read cookie function to remove a bunch of garbled. This is a tricky problem because in some cases the contents of the cookie need to be read in a scripting language on the client side. If you write in Chinese, get a bunch of garbled, is not feel very awkward? To solve this problem, but also from the way cookies are accessed.
We know that you need to escape (hexadecimal code) before writing the cookie content, and the encoding is in bytes, which is the crux of the problem: any one character will be split into two bytes and encoded separately, and in the case of a cookie, the unescape is decoded in byte units, So at last each Chinese character is turned into two bytes of garbled. What do we do? ASP or PHP can be read correctly, it should be in the unescape decoding, and in accordance with the Unicode code to spell out the Chinese characters. If so, can you find a workaround to solve the problem? After careful study, we found that as long as in the hexadecimal codec (codec) process can save Chinese character information, so we need to outsource a layer of codec process, the scheme is as follows:
1. Converts each character in the cookie string to a string of Unicode code (with a special character as the delimiter of the string) before escape coding.
2. After decoding the unescape, all the Unicode strings are extracted and then converted to the original character using the corresponding function.
Copy Code code as follows:
<script language=javascript>
<!--
Coding 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 a separator
}
return STRRTN;
}
Decoding Program:
function Decodecookie (str)
{
var Strarr;
var strrtn= "";
Strarr=str.split ("a");
for (Var i=strarr.length-1;i>=0;i--)
Strrtn+=string.fromcharcode (eval (strarr[i]));
return STRRTN;
}
-->
</script>
The VBScript version of the program is as follows:
Copy Code code as follows:
<script language=vbscript>
<!--
"Encoder:
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" "using a as separator
Next
Codecookie=strrtn
End Function
"Decoding Program:
function Decodecookie (str)
Dim I
Dim Strarr,strrtn
Strarr=split (str, "a");
For I=ubound (Strarr)-lbound (Strarr) to 1 step-1
Strrtn=strrtn & ChrW (Val (Strarr (i)))
Next
Decodecookie=strrtn
End Function
-->
</script>
For example, the cookie content you want to write is "an ABC", changed to "99a98a97a-31029a26432a" after Codecookie (based on security considerations, reverses the string), and then after the escape code becomes "99a98a97a% 2D31029A26432A "(Escape encoding converts characters other than letters and numbers to hexadecimal%xx), and note that delimiters do not select%,d and numbers. Of course, if you have important information like a password, you need to reinforce it on the encryption. Since writing cookies is generally a short message, some of the bytes added after encoding are negligible. The following JavaScript read-write cookie function adds 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 (documents.cookie.substring (j,nend)));
}
I=documents.cookie.indexof ("", i) +1;
if (i==0) break;
}
return null;
}
If you write a Chinese cookie in a CGI program, the client reads it. In the case of ASP, you can use the previously described encoding function, and then write with response, for example: Response.Cookies ("Name") =codecookie ("John") to this point, the problem of Chinese cookies is basically resolved.