Home > Backend Development > PHP Tutorial > Comparison of cookie operation methods in php and js

Comparison of cookie operation methods in php and js

WBOY
Release: 2016-07-25 08:56:49
Original
865 people have browsed it
  1. setcookie('php_cn_ck','php_中文_cookie');
  2. setcookie('php_en_ck','php_english_cookie');
  3. ?>
  4. <script> </li> <li> Cookies.set('js_cn_ck','js_中文_cookie',5000); </li> <li> Cookies.set('js_en_ck','js_english_cookie'); </li> <li>&lt ;/script> </li> <li><meta http-equiv="Content-Type" content="text/html; charset=utf8"> </li> <li>PHP cookies have been set: php_cn_ck=php_中文_cookie, php_en_ck=php_english_cookie. </li> <li>JS cookies have been set: js_cn_ck=js_中文_cookie, js_en_ck=js_english_cookie. </li> <li><a href=getcookie.php>Read cookie</a><br> </li> <li><meta http-equiv="Content-Type" content="text/html; charset=utf8"></li> </ol></div> <em onclick="copycode($('code_Ke6'));"> Copy the code </em> </div> <p>1, read the Chinese and English cookies sent by php<br><br> 1. php reads php and sets php cookie </p> <div class="blockcode"> <div id="code_LTZ"><ol> <li> <li><?php </li> <li> include('function.php'); </li> <li> $php_cn_ck=$_COOKIE['php_cn_ck']; </li> <li> $un_php_cn_ck=unescape($php_cn_ck); </li> <li> echo "Chinese cookie before decoding :php_cn_ck=$php_cn_ck<br><br>"; </li> <li> echo "Decoded Chinese cookie:un_php_cn_ck=$un_php_cn_ck<br><br>"; </li> <li> $php_en_ck=$_COOKIE['php_en_ck']; </li> <li> echo "English cookies do not need to be decoded: php_en_ck=$php_en_ck<br><br>"; </li> <li>?></li> </ol></div> <em onclick="copycode($('code_LTZ'));">Copy code</em> </div> <p>2, js reads php to set cookies<br><br> </p> <div class="blockcode"> <div id="code_lnN"><ol> <li> <li><script src="cookie.js"></script>
  5. <script> </li> <li> php_cn_ck=Cookies.get('php_cn_ck'); </li> <li> un_php_cn_ck = decodeURIComponent (escape(php_cn_ck) ) ; </li> <li> document.write("Chinese cookie before decoding: php_cn_ck="+php_cn_ck+"<Br><br>"); </li> <li> document.write("Chinese cookie after decoding: un_php_cn_ck="+un_php_cn_ck+"< Br><br>"); </li> <li> php_en_ck=Cookies.get('php_en_ck'); </li> <li> document.write("English cookies do not need to be decoded: php_en_ck="+php_en_ck+"<Br><br>"); </li> <li> </script>
Copy the code

Second, read the Chinese and English cookies sent by JS 1. PHP reads JS and sets js cookie

  1. $js_cn_ck=$_COOKIE['js_cn_ck'];
  2. $un_js_cn_ck=unescape($js_cn_ck);
  3. echo "Chinese cookie before decoding:js_cn_ck=$js_cn_ck";
  4. echo " Decoded Chinese cookie: un_js_cn_ck=$un_js_cn_ck";
  5. $js_en_ck=$_COOKIE['js_en_ck'];
  6. echo "English cookie does not need to be decoded: js_en_ck=$js_en_ck";
  7. ?>
Copy code

2, js reads the cookie set by js

  1. <script> </li> <li> js_cn_ck=Cookies.get('js_cn_ck'); </li> <li> document.write("Chinese cookie before decoding: js_cn_ck="+js_cn_ck+"<Br><br>") ; </li> <li> //un_js_cn_ck = decodeURIComponent (escape(js_cn_ck)); Calling these two sentences will cause js parsing interruption </li> <li> //document.write("Decoded Chinese cookie: un_js_cn_ck="+un_js_cn_ck+"<Br><br> "); </li> <li> js_en_ck=Cookies.get('js_en_ck'); </li> <li> document.write("English cookies do not need to be decoded: js_en_ck="+js_en_ck+"<Br><br>"); </li> <li></script>
Copy code

Summary: 1. PHP uses its own function to read PHP cookies without any obstacles and without decoding. 2. js uses the cookie.js method to read js cookies without any obstacles and without decoding. 3. To read the Chinese cookie of PHP with js, you need to do "decodeURIComponent (escape(php_cn_ck))" function processing. 4. PHP needs to use the "unescape()" function to read the Chinese cookie of JS. Code:

  1. var Cookies = {};
  2. /**
  3. * Set Cookies
  4. */
  5. Cookies.set = function(name, value){
  6. var argv = arguments;
  7. var argc = arguments.length;
  8. var expires = (argc > 2) ? argv[2] : null;
  9. if(expires != null){
  10. var exp  = new Date();
  11. exp.setTime(exp.getTime() + 8*3600 + expires) ;
  12. }
  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" : "");
  22. };
  23. /**
  24. * Read Cookies
  25. */
  26. Cookies.get = function(name){
  27. var arg = name + "=";
  28. var alen = arg.length;
  29. var clen = document.cookie.length;
  30. var i = 0;
  31. var j = 0;
  32. while(i < clen){
  33. j = i + alen;
  34. if (document.cookie.substring(i, j) == arg)
  35. return Cookies.getCookieVal(j);
  36. i = document.cookie.indexOf(" ", i) + 1;
  37. if( i == 0)
  38. break;
  39. }
  40. return null;
  41. };
  42. /**
  43. * Clear Cookies
  44. */
  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);
  50. }
  51. };
  52. Cookies.getCookieVal = function (offset){
  53. var endstr = document.cookie.indexOf(";", offset);
  54. if(endstr == -1){
  55. endstr = document.cookie.length;
  56. }
  57. return unescape(document.cookie. substring(offset, endstr));
  58. };
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template