php two functions urlencode urldecode
JS two functions decodeURI encodeURI 5.5 Previous versions are already obsolete escape unescape
A simple example is as follows:
1, PHP set cookies, JS read cookies
<?php
Setcookie ("username", UrlEncode ("Chinese Test"));
?>
<script type= "Text/javascript" >
Alert (decodeURI GetCookie ("username"))
function GetCookie (sname) {
var acookie = Document.cookie.split ('; ');
for (Var i=0 i < acookie.length; i++) {
var acrumb = acookie[i].split (' = ');
if (sname = = Acrumb[0])
Return decodeURI (acrumb[1]);
}
Return ";
}
</script>
2, JS set cookies php Read cookies
<script type= "Text/javascript" >
function Setcookie (name, value, time) {
var namestring = name + ' = ' + encodeURI (value);
var expirystring = "";
if (time!== 0) {
var expdate = new Date ();
if (time = null | | isNaN (TIME)) time = 60*60*1000;
Expdate.settime (Expdate.gettime () + time);
expirystring = '; expires = ' + expdate.togmtstring ();
}
var path = ";p ath =/";
Document.cookie = namestring + expirystring + path;
}
Setcookie ("username", "I am Chinese", 0)
</script>
<?php
Echo urldecode ($_cookie["username"]);
?>
3 JS settings js read php set PHP Read method similar
function Setcookie (name, value) {
var exp = new Date ();
Exp.settime (Exp.gettime () + 3 * 24 * 60 * 60 * 1000); 3 Days expired
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
return true;
};
function GetCookie (name) {
var arr = Document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)"));
if (arr!= null) return arr[2]; return null;
};
var currentservicealiasname = GetCookie ("AppName");
var displayName = "";
if (currentservicealiasname!= null && currentservicealiasname!= "&& currentservicealiasname!=" undef Ined ")
DisplayName = decodeURIComponent (currentservicealiasname) + "." + CounterName;
Else
DisplayName = CounterName;
decodeURIComponent Use this to convert
Summary: Cookies to solve the Chinese garbled problem is essentially the conversion between the URL encoding functions, small series summed up the fact that the cookie is best not to use Chinese, Chinese in these places to deal with is not enough.