本文实例讲述了PHP与JavaScript针对Cookie的读写、交互操作方法。分享给大家供大家参考,具体如下:
下面的例子列出几种情形交互场景,列出JS和php交互的方法。总结下,以免日后再为cookie问题困扰。
<?php setcookie('php_cn_ck','php_中文_cookie'); setcookie('php_en_ck','php_english_cookie'); ?>
<script src="cookie.js"></script> <script> Cookies.set('js_cn_ck','js_中文_cookie',5000); Cookies.set('js_en_ck','js_english_cookie'); </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
<?php 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<br><br>"; echo "解码后的中文cookie:un_php_cn_ck=$un_php_cn_ck<br><br>"; $php_en_ck=$_COOKIE['php_en_ck']; echo "英文cookie无需解码:php_en_ck=$php_en_ck<br><br>"; ?>
2 js读取php设置cookie
<script src="cookie.js"></script> <script> php_cn_ck=Cookies.get('php_cn_ck'); un_php_cn_ck = decodeURIComponent (escape(php_cn_ck)); document.write("解码前的中文cookie :php_cn_ck="+php_cn_ck+"<Br><br>"); document.write("解码后的中文cookie :un_php_cn_ck="+un_php_cn_ck+"<Br><br>"); php_en_ck=Cookies.get('php_en_ck'); document.write("英文cookie无需解码 :php_en_ck="+php_en_ck+"<Br><br>"); </script>
二 读取JS传送的中英文cookie
1 php读取JS设置js cookie
<?php $js_cn_ck=$_COOKIE['js_cn_ck']; $un_js_cn_ck=unescape($js_cn_ck); echo "解码前的中文cookie:js_cn_ck=$js_cn_ck<br><br>"; echo "解码后的中文cookie:un_js_cn_ck=$un_js_cn_ck<br><br>"; $js_en_ck=$_COOKIE['js_en_ck']; echo "英文cookie无需解码:js_en_ck=$js_en_ck<br><br>"; ?>
2 js读取js设置的cookie
<script> js_cn_ck=Cookies.get('js_cn_ck'); document.write("解码前的中文cookie :js_cn_ck="+js_cn_ck+"<Br><br>"); //un_js_cn_ck = decodeURIComponent (escape(js_cn_ck)); 调用这两句会出现js解析中断 //document.write("解码后的中文cookie :un_js_cn_ck="+un_js_cn_ck+"<Br><br>"); js_en_ck=Cookies.get('js_en_ck'); document.write("英文cookie无需解码 :js_en_ck="+js_en_ck+"<Br><br>"); </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()" 函数处理
cookie.js:
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){ var exp = new Date(); exp.setTime(exp.getTime() + 8*3600 + expires); } 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; while(i < clen){ 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)); };