PHP set of Chinese Cookiejs get garbled solution

Source: Internet
Author: User
Tags setcookie urlencode

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.

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.