-
-
setcookie('php_cn_ck','php_中文_cookie');
- setcookie('php_en_ck','中文_cookie');
- setcookie('php_en_ck','中文_cookie');
- ?>
-
-
<script> <li> Cookies.set('js_cn_ck','js_中文_cookie',5000); <li> Cookies.set('js_en_ck','js_english_cookie'); <li></script>
-
- PHP cookie已經設定:php_cn_ck=php_中文_cookie、php_en_ck=php_english_cookie。
- JS cookie已經設定:js_cn_ck=js_中文_cookie、js_en_ck=js_english_cookie。
-
讀取cookie
複製程式碼
一,讀取php傳送的中英文cookie
1,php讀取php設定php cookie
-
-
-
include('function.php');
- $php_cn_ck=$_COOKIE['php_cn_ck']; $un_php_cn_ck=unescape($php_cn_ck);
- echo "解碼前的中文cookie:php_cn_ck=$php_cn_ck
";
- echo "解碼後的中文cookie:un_php_cn_ck=$un_&cn_ck=$un_&cn_ck
";
- $php_en_ck=$_COOKIE['php_en_ck'];
- echo "英文cookie無解碼:php_en_ck=$php_en_ck
";
- ?>
複製程式碼
2,js讀取php設定cookie
-
-
- <script> <li> php_cn_ck=Cookies.get('php_cn_ck' ); <li> un_php_cn_ck = decodeURIComponent (escape(php_cn_ck)); <li> document.write("解碼前的中文cookie :php_cn_ck="+php_cn_ck+"<Br><br>"); ("解碼後的中文cookie :un_php_cn_ck="+un_php_cn_ck+"<Br><br>"); <li> php_en_ck=Cookies.get('php_en_ck'); <li> document.write("英文cookie php_en_ck="+php_en_ck+"<Br><br>"); <li></script>
-
複製程式碼
二,讀取JS傳送的中英文cookie
1,php讀取JS設定js cookie
-
$js_cn_ck=$_COOKIE['js_cn_ck'];
- $un_js_cnck=ckscape($js_ck); echo "解碼前的中文cookie:js_cn_ck=$js_cn_ck";
- echo "解碼後的中文cookie:un_js_cn_ck=$un_js_cn_ck";
- $js_en_ck=$_COOKIE['js_ck_ck";
- $js_en_ck=$_COOKIE['js_en_ck";
- $js_en_ck=$_COOKIE['js_en_ck"; $js_en_ck=$_COOKIE['js_en_ck"; ?>
-
複製程式碼
2,jsjs設定設定的cookie
-
<script> <li> js_cn_ck=Cookies.get('js_cn_ck'); <li> document.write("解碼前的中文」解碼前的中文_C_ck_ck_c: ="+js_cn_ck+"<Br><br>"); <li> //un_js_cn_ck = decodeURIComponent (escape(js_cn_ck)); 呼叫這兩句會出現js解析中斷<li> //document.write("解碼後的中文cookie :un_js_cn_ck="+un_js_cn_ck+"<Br><br>"); <li> js_en_ck=Cookies.get('js_en_ck'); <li> document.write("英文cookie無解碼:js_en_ck="+js_ "<Br><br>"); <li></script>
-
-
複製程式碼 總結:
1,php用自身函數讀取php 的cookie,沒有任何障礙,無需解碼處理。
2,js採用cookie.js方法讀取js 的cookie,沒有任何障礙,無需解碼處理。
3,js讀取php的中文cookie,需要做 "decodeURIComponent (escape(php_cn_ck)) "函式處理。
4,php讀取js的中文cookie 需要做 "unescape()" 函式處理。
代碼:
-
- var Cookies = {};
- /**
- * 設定Cookies
- */
- Cookies.set = function(name, value){
- var argv = arguments;
- var argc = arguments.length;
- var expires = (argc > 2) ? argv[2] : null;
- if(expires != null){
- if(expires != null){
- if(expires != null){
- if(expires ! var exp = new Date();
- exp.setTime(exp.getTime() + 8*3600 + expires);
- }
- alert(exp.toGMTString());
- }
- alert(exp.toGMTString());
- var path = (argc > 3) ? argv[3] : '/';
- var domain = (argc > 4) ? argv[4] : null;
- var secure = (argc > 5) ? argv[5] : false;
- document.cookie = name + "=" + escape (value) +
- ((expires == null) ? "" : ("; expires=" + exp.toGMTString())) +
- ((path == null) ? "" : ("; path=" + path)) +
- ((domain == null) ? "" : ("; domain=" + domain)) +
- ((secure == true) ? "; secure" : "");
- };
- /**
- * 讀取Cookies
- */
- Cookies.get = function(name){
- var arg = name + "=";
- var alen = arg.length;
- var clen = document.cookie.length;
- var i = 0;
- var j = 0;
- var i = 0;
- var j = 0;
- while(i j = i + alen;
- if (document.cookie.substring(i, j) == arg)
- return Cookies.getCookieVal(j);
- i = document.cookie.indexOf(" ", i) + 1;
- if(i == 0)
- break;
- }
- return null;
- };
- / **
- * 清除Cookies
- */
- Cookies.clear = function(name) {
- if(Cookies.get(name)){
- var expdate = new Date();
- expdate.setTime( expdate.getTime() - (86400 * 1000 * 1));
- Cookies.set(name, "", expdate);
- }
- };
- Cookies.getCookieVal = function(offset) {
var endstr = document.cookie.indexOf(";", offset); if(endstr == -1){ endstr = document.cookie.length; } return unescape(document.cookie.substring(offset, endstr));
| };