Use Javascript to read the Chinese COOKIE_PHP tutorial set in PHP-php Tutorial

Source: Internet
Author: User
Tags php write
Use Javascript to read Chinese cookies set in PHP. LieHuo. Net PHP Tutorial PHP program writes some Chinese data in the COOKIE, but cannot be correctly displayed when reading Javascript? I believe many of my friends have met this situation. Tutorial on PHP of Kangli website building Institute (Bkjia. Com)The PHP program writes some Chinese data in the COOKIE, but it cannot be correctly displayed after reading it using Javascript? I believe a lot of friends have encountered this situation, the day before yesterday, find a solution: PHP write COOKIE in UTF-8 encoding to write, javascript can also be decoded when it is read, because Javascript uses Unicode encoding internally.

Reference content is as follows:
/**
*
* URL encode/decode
*
**/

Var Url = {

// Public method for url encoding
Encode: function (string ){
Return escape (this. _ utf8_encode (string ));
},

// Public method for url decoding
Decode: function (string ){
Return this. _ utf8_decode (unescape (string ));
},

// Private method for UTF-8 encoding
_ Utf8_encode: function (string ){
String = string. replace (/\ r \ n/g, "\ n ");
Var utftext = "";

For (var n = 0; n <string. length; n ++ ){

Var c = string. charCodeAt (n );

If (c <128 ){
Utftext + = String. fromCharCode (c );
}
Else if (c> 127) & (c <2048 )){
Utftext + = String. fromCharCode (c> 6) | 192 );
Utftext + = String. fromCharCode (c & 63) | 128 );
}
Else {
Utftext + = String. fromCharCode (c> 12) | 224 );
Utftext + = String. fromCharCode (c> 6) & 63) | 128 );
Utftext + = String. fromCharCode (c & 63) | 128 );
}

}

Return utftext;
},

// Private methods for UTF-8 decoding
_ Utf8_decode: function (utftext ){
Var string = "";
Var I = 0;
Var c = c1 = c2 = 0;

While (I <utftext. length ){

C = utftext. charCodeAt (I );

If (c <128 ){
String + = String. fromCharCode (c );
I ++;
}
Else if (c> 191) & (c <224 )){
C2 = utftext. charCodeAt (I + 1 );
String + = String. fromCharCode (c & 31) <6) | (c2 & 63 ));
I + = 2;
}
Else {
C2 = utftext. charCodeAt (I + 1 );
C3 = utftext. charCodeAt (I + 2 );
String + = String. fromCharCode (c & 15) <12) | (c2 & 63) <6) | (c3 & 63 ));
I + = 3;
}

}

Return string;
}

}

/**
* This section reads the Cookie.
*/
Function GetCookie (sName)
{
Var mycookie = document. cookie. split (";");
For (var I = 0; I <mycookie. length; I ++)
{
Var cookie1 = mycookie [I]. split ("= ");
If (cookie1 [0] = sName)
{
Return Url. decode (cookie1 [1]);
}
}
Return null;
}

In the LieHuo. Net PHP Tutorial, the PHP program writes some Chinese data in the COOKIE, but does not display the data correctly when reading it using Javascript? I believe many of my friends have met this kind of situation...

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.