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...