Client Cookie Chinese programming (on)

Source: Internet
Author: User
Tags split client
Cookie| Programming | client | Chinese client Cookies Chinese programming (from yesky.com)

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.

<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 Trrtn & "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>




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.