Cookie operations in PHP and JavaScript

Source: Internet
Author: User
The following example lists the interaction scenarios in several cases and the Interaction methods between JS and php. To avoid further cookie problems. Setcookie. phpViewCodegetcookie. phpViewCode Summary: php uses its own function to read php cookies. There is no obstacle and no decoding is required. Js uses the cookie. js method to read the js cooki

The following example lists the interaction scenarios in several cases and the Interaction methods between JS and php. To avoid further cookie problems. Setcookie. php View Code getcookie. php View Code Summary: php uses its own function to read php cookies. There is no obstacle and no decoding is required. Js uses the cookie. js method to read the js cooki

The following example lists the interaction scenarios in several cases and the Interaction methods between JS and php. To avoid further cookie problems.

Setcookie. php

View Code

Getcookie. php

View Code

Summary:

  1. Php uses its own functions to read php cookies. There is no obstacle and no decoding is required.
  2. Js uses the cookie. js method to read js cookies. There is no obstacle and no decoding is required.
  3. Js reads php Chinese cookies and requires "decodeURIComponent (escape (php_cn_ck)" function processing.
  4. Php needs to perform the "unescape ()" function to read the Chinese cookies of js.

Cookie. js

Var Cookies = {};/***** set 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": "") ;};/***** read 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 ;};/*** 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 = document. cookie. length;} return unescape (document. cookie. substring (offset, endstr ));};

4 function. php

View Code

  

Bytes

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.