Home  >  Article  >  Backend Development  >  Chinese COOKIE_PHP tutorial for reading PHP settings using Javascript

Chinese COOKIE_PHP tutorial for reading PHP settings using Javascript

WBOY
WBOYOriginal
2016-07-21 14:56:20861browse

Agni Website Building Academy (Bkjia.Com) PHP Tutorial The PHP program writes some Chinese data in COOKIE, but when it is read with Javascript, it is read and cannot be displayed correctly? I believe many friends have encountered this situation. I searched the day before yesterday and found a solution: PHP writes COOKIE in UTF-8 encoding, and JS just decodes it when reading it out, because Javascript uses it internally. It is Unicode encoding.

The following is the quoted content:
/**
*
* 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(/rn/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 method 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) {
191) && (c < i++;
}
else if((c > 191) && (c < 224)) {
             c2 = utftext.charCodeAt(i+1);
             string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
          i += 2;​ string += String .fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
}

}

/**
* This section is for reading 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]);
                                                                                                                                                


http://www.bkjia.com/PHPjc/364219.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/364219.htmlTechArticleLieHuo.Net PHP Tutorial The PHP program writes some Chinese data in COOKIE, and uses Javascript When reading, it is not displayed correctly? I believe many friends have encountered this situation...
Statement:
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