Client Cookie Chinese Programming

Source: Internet
Author: User
Tags split client
Cookie| Programming | client | chinese


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), use a server-side program such as ASP
or PHP) is completely correct, but a bunch of garbled characters are taken out of the normal JavaScript or VBScript read cookie function. It's a tricky question,
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 and get a bunch of garbled, isn't it feeling very
Is it 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 key to the problem.
In: Any Chinese character will be split into two bytes encoded separately, while reading cookies, unescape again in byte unit decoding, so the last of each character is changed
into a two-byte garbled. What do we do? ASP or PHP can be read correctly, should be in the unescape after decoding, and in accordance with the Unicode encoding of the Han
Words spelled out. If so, can you find a workaround to solve the problem? Careful study found that as long as the hexadecimal codec (edited
Code) process can save Chinese character information, so we need to outsource a layer of codec process, the program is as follows:

1. Converts each character in the cookie string to a string of Unicode codes (separated by a special character as String) before escape coding
character).

2. After decoding the unescape, all the Unicode strings are extracted and then converted to the original character using the corresponding function.

<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:

<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," which, after Codecookie, becomes "99a98a97a-31029a26432a" (based on security considerations, the
string inversion, and then after escape encoded into "99a98a97a%2d31029a26432a" (Escape encoding converts characters except letters and numbers to 10
In the form of a%xx), note that the separator cannot select%,d and digits. Of course, if you have important information like a password, you need to reinforce it on the encryption. Because
For writing cookies, it is generally short messages, and some of the bytes added after encoding are negligible. The following JavaScript read-write cookie function adds the above
Support in Chinese.

function Setcookie (name,value,expires)
{
var exp=new Date ();
Exp.settime (Exp.gettime () +expires*60*1000);
document.cookie=name+ "=" +escape (Codecookie (value)) + "; Expires= "+exp.togmtstring () +"; path=/";
}

function GetCookie (name)
{
var strarg=name+ "=";
var narglen=strarg.length;
var ncookielen=document.cookie.length;
var nend;
var i=0;
var J;

while (I<ncookielen)
{
J=i+narglen;
if (document.cookie.substring (i,j) ==strarg)
{
Nend=document.cookie.indexof (";", j);
if (nend==-1) nend=document.cookie.length;
Return Decodecookie (Unescape (document.cookie.substring (j,nend)));
}
I=document.cookie.indexof ("", i) +1;
if (i==0) break;
}
return null;
}

If you write a Chinese cookie in a CGI program, the client reads it. such as in the ASP, you can first use the preceding coding function, and then write with response, example
such as: Response.Cookies ("Name") =codecookie ("John") to this point, the problem of Chinese cookies are basically resolved. If you have any related questions, please come to
Www.coolbel.com/saibai communication.




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.