下面的例子列出几种情形交互场景,列出JS和php交互的方法。总结下,以免日后再为cookie问题困扰。
setcookie.php
02 | setcookie( 'php_cn_ck' , 'php_中文_cookie' ); |
03 | setcookie( 'php_en_ck' , 'php_english_cookie' ); |
06 | <script src= "cookie.js" ></script> |
08 | Cookies.set( 'js_cn_ck' , 'js_中文_cookie' ,5000); |
09 | Cookies.set( 'js_en_ck' , 'js_english_cookie' ); |
12 | <meta http-equiv= "Content-Type" content= "text/html; charset=utf8" > |
13 | PHP cookie已经设置<br>php_cn_ck=php_中文_cookie<br>php_en_ck=php_english_cookie<br><br> |
14 | JS cookie已经设置<br>js_cn_ck=js_中文_cookie<br>js_en_ck=js_english_cookie<br><br> |
15 | <a href=getcookie.php>读取cookie</a><br> |
getcookie.php
01 | <meta http-equiv= "Content-Type" content= "text/html; charset=utf8" > |
02 | 一 读取php传送的中英文cookie<br><br> |
03 | <p>1 php读取php设置php cookie<br><br> |
06 | include ( 'function.php' ); |
07 | $php_cn_ck = $_COOKIE [ 'php_cn_ck' ]; |
08 | $un_php_cn_ck =unescape( $php_cn_ck ); |
09 | echo "解码前的中文cookie:php_cn_ck=$php_cn_ck<br><br>" ; |
10 | echo "解码后的中文cookie:un_php_cn_ck=$un_php_cn_ck<br><br>" ; |
11 | $php_en_ck = $_COOKIE [ 'php_en_ck' ]; |
12 | echo "英文cookie无需解码:php_en_ck=$php_en_ck<br><br>" ; |
15 | <p>2 js读取php设置cookie<br><br> |
16 | <script src= "cookie.js" ></script> |
18 | php_cn_ck=Cookies.get( 'php_cn_ck' ); |
19 | un_php_cn_ck = decodeURIComponent (escape(php_cn_ck)); |
20 | document.write( "解码前的中文cookie :php_cn_ck=" +php_cn_ck+ "<Br><br>" ); |
21 | document.write( "解码后的中文cookie :un_php_cn_ck=" +un_php_cn_ck+ "<Br><br>" ); |
22 | php_en_ck=Cookies.get( 'php_en_ck' ); |
23 | document.write( "英文cookie无需解码 :php_en_ck=" +php_en_ck+ "<Br><br>" ); |
25 | -----------------------------------------------<br> |
26 | 二 读取JS传送的中英文cookie<br><br> |
27 | <p>1 php读取JS设置js cookie<br><br> |
29 | $js_cn_ck = $_COOKIE [ 'js_cn_ck' ]; |
30 | $un_js_cn_ck =unescape( $js_cn_ck ); |
31 | echo "解码前的中文cookie:js_cn_ck=$js_cn_ck<br><br>" ; |
32 | echo "解码后的中文cookie:un_js_cn_ck=$un_js_cn_ck<br><br>" ; |
33 | $js_en_ck = $_COOKIE [ 'js_en_ck' ]; |
34 | echo "英文cookie无需解码:js_en_ck=$js_en_ck<br><br>" ; |
37 | <p>2 js读取js设置的cookie<br><br> |
39 | js_cn_ck=Cookies.get( 'js_cn_ck' ); |
40 | document.write( "解码前的中文cookie :js_cn_ck=" +js_cn_ck+ "<Br><br>" ); |
41 | //un_js_cn_ck = decodeURIComponent (escape(js_cn_ck)); 调用这两句会出现js解析中断 |
42 | //document.write("解码后的中文cookie :un_js_cn_ck="+un_js_cn_ck+"<Br><br>"); |
43 | js_en_ck=Cookies.get( 'js_en_ck' ); |
44 | document.write( "英文cookie无需解码 :js_en_ck=" +js_en_ck+ "<Br><br>" ); |
总结:
- php用自身函数读取php 的cookie,没有任何障碍,无需解码处理。
- js采用cookie.js方法读取js 的cookie,没有任何障碍,无需解码处理。
- js读取php的中文cookie,需要做 "decodeURIComponent (escape(php_cn_ck)) "函数处理
- php读取js的中文cookie 需要做 "unescape()" 函数处理
cookie.js
05 | Cookies.set = function (name, value){ |
07 | var argc = arguments.length; |
08 | var expires = (argc > 2) ? argv[2] : null ; |
11 | exp.setTime(exp.getTime() + 8*3600 + expires); |
13 | alert(exp.toGMTString()); |
14 | var path = (argc > 3) ? argv[3] : '/' ; |
15 | var domain = (argc > 4) ? argv[4] : null ; |
16 | var secure = (argc > 5) ? argv[5] : false ; |
17 | document.cookie = name + "=" + escape (value) + |
18 | ((expires == null ) ? "" : ( "; expires=" + exp.toGMTString())) + |
19 | ((path == null ) ? "" : ( "; path=" + path)) + |
20 | ((domain == null ) ? "" : ( "; domain=" + domain)) + |
21 | ((secure == true ) ? "; secure" : "" ); |
26 | Cookies.get = function (name){ |
28 | var alen = arg.length; |
29 | var clen = document.cookie.length; |
34 | if (document.cookie.substring(i, j) == arg) |
35 | return Cookies.getCookieVal(j); |
36 | i = document.cookie.indexOf( " " , i) + 1; |
45 | Cookies.clear = function (name) { |
46 | if (Cookies.get(name)){ |
47 | var expdate = new Date(); |
48 | expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); |
49 | Cookies.set(name, "" , expdate); |
52 | Cookies.getCookieVal = function (offset){ |
53 | var endstr = document.cookie.indexOf( ";" , offset); |
55 | endstr = document.cookie.length; |
57 | return unescape(document.cookie.substring(offset, endstr)); |