Home > Web Front-end > JS Tutorial > body text

Solution to reading Chinese COOKIE with Javascript_javascript skills

WBOY
Release: 2016-05-16 19:18:59
Original
1231 people have browsed it

In the use of cookies, we found such a problem: if the content written in the cookie is in Chinese (such as the user's name), it is completely correct to read it with a server-side program (such as ASP or PHP), but it is completely correct to read it with general javascript or VBScript. What the Cookie function takes out is a bunch of gibberish. This is a tricky problem because in some cases the contents of the cookie need to be read using a scripting language on the client side. If you write Chinese and get a bunch of garbled characters, does it feel awkward? To solve this problem, we have to start with the cookie access method.
We know that escape (hexadecimal encoding) is required before writing the cookie content, and the encoding is in bytes. This is the crux of the problem: any Chinese character will be split into two characters. Each section is encoded separately; when reading the cookie, unescape decodes it in byte units, so in the end each Chinese character becomes two bytes of garbled code. What to do? If the ASP or PHP method can be read correctly, it should be that after unescape decoding, the Chinese characters are spelled out according to the unicode encoding. If this is the case, can you find a workaround to solve this problem? After careful study, we found that as long as the Chinese character information can be saved during the hexadecimal codec (encoding and decoding) process, we need to outsource another layer of codec process. The plan is as follows:

 1. Before escape encoding , convert each character in the Cookie string into the string form of Unicode code (using a special character as the string separator).

2. After unescape decoding, first extract all unicode strings, and then use the corresponding function to convert them into original characters.

Copy code The code is as follows:




The VBScript version of the program is as follows:

Copy the code The code is as follows:

< SCRIPT LANGUAGE=vbscript>




예를 들어 작성하려는 쿠키 내용은 "一丁ABC"인데 CodeCookie 이후에는 "99a98a97a-31029a26432a"로 변경되고(보안상의 이유로 문자열은 반전됨) 이후에는 "99a98a97a-31029a26432a"가 됩니다. "(이스케이프 인코딩은 문자와 숫자 이외의 문자를 16진수 %XX 형식으로 변환합니다.) 구분 기호는 %, D 및 숫자일 수 없습니다. 물론, 비밀번호 등 중요한 정보가 있는 경우에는 암호화를 강화해야 합니다. 쿠키는 일반적으로 짧은 메시지로 작성되므로 인코딩 후 추가되는 바이트 수는 무시할 수 있습니다. 위 중국어 지원이 다음 자바스크립트 읽기 및 쓰기 쿠키 기능에 추가되었습니다.

function SetCookie(name,value,expires)
{
var exp=new Date()
exp.setTime(exp.getTime()expires*60*1000); 🎜> document.cookie=name "=" escape(CodeCookie(value)) " ; 만료=" exp.toGMTString() " ; path=/";
}

function GetCookie(이름)
{
var strArg=name "="
var nArgLen=strArg.length;
var nCookieLen=documents.cookie.length;
var nEnd;
var j;

while (i{
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)
}
return null;
}


CGI 프로그램을 사용하여 중국어 쿠키를 작성하면 클라이언트가 이를 읽습니다. 예를 들어 ASP에서는 앞서 언급한 인코딩 기능을 먼저 사용한 다음 응답으로 작성할 수 있습니다. 예: response.cookies("Name")=CodeCookie("Zhang San") 이때 중국 쿠키의 문제점은 다음과 같습니다. 기본적으로 해결되었습니다.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template