Home Web Front-end JS Tutorial Solution to reading Chinese COOKIE with Javascript_javascript skills

Solution to reading Chinese COOKIE with Javascript_javascript skills

May 16, 2016 pm 07:18 PM

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") 이때 중국 쿠키의 문제점은 다음과 같습니다. 기본적으로 해결되었습니다.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How do I use Java's collections framework effectively? How do I use Java's collections framework effectively? Mar 13, 2025 pm 12:28 PM

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Mar 15, 2025 am 09:19 AM

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

See all articles