Php+javascript implementation of Cookie read-write, interactive operation method detailed

Source: Internet
Author: User
Tags setcookie
This article mainly introduces PHP and JavaScript for the cookie read and write, interactive operation method, in combination with the example of PHP and JavaScript set cookies, PHP read PHP and JS settings cookies, JS read php and JS set cookies and other related operating skills, the need for friends can refer to the next

The examples in this paper describe how PHP and JavaScript read, write, and interact with cookies. Share to everyone for your reference, as follows:

The following example lists several scenario interaction scenarios, listing the ways in which JS and PHP interact. In summary, in order to avoid future problems with cookies.

<?php Setcookie (' Php_cn_ck ', ' php_ Chinese _cookie '); Setcookie (' Php_en_ck ', ' Php_english_cookie ');? >

<script src= "Cookie.js" ></script><script> cookies.set (' Js_cn_ck ', ' js_ Chinese _cookie ', 5000); Cookies.set (' Js_en_ck ', ' Js_english_cookie ');</script>

The PHP cookie has been set:
Php_cn_ck=php_ Chinese _cookie
Php_en_ck=php_english_cookie

JS Cookie has been set:
Js_cn_ck=js_ Chinese _cookie
Js_en_ck=js_english_cookie

Read cookies

A read PHP in Chinese and English cookies sent

1 php read PHP set PHP cookie

<?php include (' function.php '); $php _cn_ck=$_cookie[' Php_cn_ck ']; $un _php_cn_ck=unescape ($php _cn_ck); echo "Chinese cookie:php_cn_ck= before decoding $php _cn_ck<br><br>"; echo "Decoded Chinese cookie:un_php_cn_ck= $un _php_cn_ck<br><br>"; $php _en_ck=$_cookie[' Php_en_ck ']; echo "English cookie without decoding: php_en_ck= $php _en_ck<br><br>";? >

2 js read PHP settings 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 ("Chinese cookie before decoding:p hp_cn_ck=" +php_cn_ck+ "<Br><br>"); document.write ("Decoded Chinese cookie:un_php_cn_ck=" +un_php_cn_ck+ "<Br><br>"); Php_en_ck=cookies.get (' Php_en_ck '); document.write ("English cookie does not need to decode:p hp_en_ck=" +php_en_ck+ "<Br><br>");</script>

Two read JS sent in Chinese and English cookies

1 php Read JS set JS cookie

<?php $js _cn_ck=$_cookie[' Js_cn_ck ']; $un _js_cn_ck=unescape ($js _cn_ck); echo "Chinese cookie:js_cn_ck= before decoding $js _cn_ck<br><br>"; echo "Decoded Chinese cookie:un_js_cn_ck= $un _js_cn_ck<br><br>"; $js _en_ck=$_cookie[' Js_en_ck ']; echo "English cookie without decoding: js_en_ck= $js _en_ck<br><br>";? >

2 JS read JS settings cookie

<script> js_cn_ck=cookies.get (' Js_cn_ck '); document.write ("Chinese cookie:js_cn_ck= before decoding" +js_cn_ck+ "<Br><br>"); Un_js_cn_ck = decodeURIComponent (Escape (Js_cn_ck)); Call these two sentences will appear JS parsing interrupt//document.write ("Decoded Chinese cookie:un_js_cn_ck=" +un_js_cn_ck+ "<Br><br>"); Js_en_ck=cookies.get (' Js_en_ck '); document.write ("English cookie does not need decoding: js_en_ck=" +js_en_ck+ "<Br><br>");</script>

Summarize:

1. PHP uses its own function to read PHP cookies, without any obstacles, without decoding processing.
2. JS uses the Cookie.js method to read the JS cookie, without any obstacles, without decoding processing.
3. JS read PHP's Chinese cookie, need to do "decodeuricomponent (Escape (PHP_CN_CK))" Function processing
4. php Read JS Chinese cookie needs to do "unescape ()" Function processing

Cookie.js:

var Cookies = {};/*** Settings cookies*/cookies.set = function (name, value) {var argv = arguments; var argc = arguments.length; v AR 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 ":" ");};/ Read Cookies*/cookies.get = function (name) {var arg = name + "="; var alen = arg.length; var Clen = Document.cookie.lengt H 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;};/ Clear 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 = do Cument.cookie.length; } return Unescape (document.cookie.substring (offset, endstr));};

Related recommendations:

Cookie use case in PHP

jquery+Cookie Switch Skin step

Cookie Plugin Js-cookie use case explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.