js和php的cookie交互方法 - 小众知识

js和php的cookie交互方法

2014-09-24 05:43:21 苏内容
  标签: js/php/cookie/交互
阅读:3414
最近开发大量采用了EXT.JS和PHP,所以这两种语言之间的交互非常多.cookie是最适合他们交互的方式.

下面的例子列出4种情形交互场景,列出JS和php交互的方法.总结下.以免日后再为cookie问题困扰


1 setcookie.php

<?php   //上面不能有空行
 setcookie('php_cn_ck','php_中文_cookie');
 setcookie('php_en_ck','php_english_cookie');
 ?>
 <script src="cookie.js"></script>
 <script>
 set_cookie('js_cn_ck','js_中文_cookie');
 set_cookie('js_en_ck','js_english_cookie');
 </script>
 <html>
 <meta http-equiv="Content-Type" content="text/html; charset=utf8">
 PHP cookie已经设置<br>php_cn_ck=php_中文_cookie<br>php_en_ck=php_english_cookie<br><br>
 JS cookie已经设置<br>js_cn_ck=js_中文_cookie<br>js_en_ck=js_english_cookie<br><br>
 <a href=getcookie.php>读取cookie</a><br>
 </html>


2  getcookie.php

<meta http-equiv="Content-Type" content="text/html; charset=utf8">
 一 读取php传送的中英文cookie<br><br>
 <p>1 php读取php设置php cookie<br><br>
 <?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>";
 ?>
 <p>2 js读取php设置cookie<br><br>
 <script src="cookie.js"></script>
 <script>
 php_cn_ck=get_cookie('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=get_cookie('php_en_ck');
 document.write("英文cookie无需解码 :php_en_ck="+php_en_ck+"<Br><br>");
 </script>
 -----------------------------------------------<br>
 二 读取JS传送的中英文cookie<br><br>
 <p>1 php读取JS设置js cookie<br><br>
 <?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>";
 ?>
 </p>
 <p>2 js读取js设置的cookie<br><br>
 <script>
 js_cn_ck=get_cookie('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=get_cookie('js_en_ck');
 document.write("英文cookie无需解码 :js_en_ck="+js_en_ck+"<Br><br>");
 </script>
 </p><pre>

总结: 1 php用自身函数读取php 的cookie,没有任何障碍,无需解码处理.
  2 js采用cookie.js方法读取js 的cookie,没有任何障碍,无需解码处理.
  
  3 js读取php的中文cookie,需要做 "decodeURIComponent (escape(php_cn_ck)) "函数处理
  
  4 php读取js的中文cookie 需要做 " unescape()" 函数处理
</pre>

文件中的函数包文件如下

3 cookie.js

function set_cookie(name, value, expires, path, domain, secure) 
 {
 // set time, it's in milliseconds
 var today = new Date();
 today.setTime( today.getTime() );

 if ( expires )
 {
 expires = expires * 1000 * 60 * 60 * 24;
 }
 var expires_date = new Date( today.getTime() + (expires) );

 document.cookie = name + "=" +escape( value ) +
 ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
 ( ( path ) ? ";path=" + path : "" ) + 
 ( ( domain ) ? ";domain=" + domain : "" ) +
 ( ( secure ) ? ";secure" : "" );
 }
 /*
 * This will retrieve the cookie by name, if the cookie does not exist, it will return false, so you can do things like 
 * if ( Get_Cookie( 'your_cookie' ) ) do something.
 */
 function get_cookie(name) {
 var start = document.cookie.indexOf(name + "=");
 var len = start + name.length + 1;
 if ((!start) && (name != document.cookie.substring(0, name.length )))
 {
   return null;
 }
 if (start == -1) return null;
 var end = document.cookie.indexOf(";", len);
 if (end == -1) end = document.cookie.length;
 return unescape(document.cookie.substring(len, end));
 }


4 function.php

function unescape($str) {
 $str = rawurldecode ( $str );
 preg_match_all ( "/(?:%u.{4})|&#x.{4};|&#d+;|.+/U", $str, $r );
 $ar = $r [0];
 foreach ( $ar as $k => $v ) {
   if (substr ( $v, 0, 2 ) == "%u") {
    if (! eregi ( "WIN", PHP_OS )) {
     $tmpString = "%u" . substr ( $v, 4, 2 ) . substr ( $v, 2, 2 );
    }
    $ar [$k] = iconv ( "UCS-2", "utf-8", pack ( "H4", substr ( $tmpString, - 4 ) ) );
   } elseif (substr ( $v, 0, 3 ) == "&#x")
    $ar [$k] = iconv ( "UCS-2", "utf-8", pack ( "H4", substr ( $v, 3, - 1 ) ) );
   elseif (substr ( $v, 0, 2 ) == "&#") {
    $ar [$k] = iconv ( "UCS-2", "utf-8", pack ( "n", substr ( $v, 2, - 1 ) ) );
   }
 }
 return join ( "", $ar );
 }

扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1