These two days do shopping cart logic. In accordance with the usual practice, the pre-order information in the cookie, the result of a lot of unreasonable things, the complete proof of my ignorance of the cookie.
。
。
So many years. Very few cookies are used because it is considered unsafe. However, in some cases, using cookies can greatly simplify the logic and system burden, such as login verification and shopping carts, designed and handled only well, there is no security issues.
It is because of the use of less. Occasionally used, but also only to touch the fur, therefore, the understanding and mastery of the cookie is very limited, some ignorant place, this time is exposed.
。。
The following is a summary of the problems encountered, to give you some reference, it is not necessarily right. Is the experience of their own test.
1. The cookie assumes that the specified expiration time is 0, that is, the period is perpetual and cannot be deleted.
So you want to change and delete cookies, initially created, the expiration time must give a certain value, do not give 0 value.
This is a pit, the document does not explain, killing the dead.
。。
2. Cookies are not only controlled by expiration time. You can also control the scope, the role page path.
But for security reasons. The scope cannot be manipulated with JS and must be operated on the server side.
This is pit number second.
。。 Give the participation, but can not use, do not explain, very harmful ....
And the function of the page can be specified with JS.
3. When deleting cookies, note that when the path is created, it should be removed, or it will not be deleted.
Here is the source code for the jquery.cookie.js found online:
(function (Factory) {if (typeof define = = = ' function ' && define.amd) {define ([' jquery '], factory); } else if (typeof exports = = = ' object ') {Module.exports = Factory (Require (' jquery ')); } else {factory (jQuery); }} (function ($) {var pluses =/\+/g; function encode (s) {return config.raw? S:encodeuricomponent (s); } function decode (s) {return config.raw? s:decodeuricomponent (s); } function Stringifycookievalue (value) {return encode (Config.json? Json.stringify (value): String (value)); } function Parsecookievalue (s) {if (S.indexof (' "') = = = 0) {s = s.slice (1,-1). replace (/\\"/g, ' "'). Replace (/\\\\/g, ' \ \ '); } try {s = decodeuricomponent (S.replace (pluses, ")); Return Config.json? Json.parse (s): s; } catch (E) {}} function read (s, converter) {var value = Config.raw? S:parsecookievalue (s); Return $.isfunction (Converter)? Converter (value): value; } var config = $.cookie = function (key, value, options) {if (Arguments.length > 1 &&!$.isfunction ( Value) {options = $.extend ({}, Config.defaults, options); if (typeof options.expires = = = ' number ') {var days = options.expires, T = OptioNs.expires = new Date (); T.setmilliseconds (T.getmilliseconds () + days * 864e+5); } return (Document.cookie = [Encode (key), ' = ', Stringifycookievalue (value), Options.expires? '; Expires= ' + options.expires.toUTCString (): ", Options.path?
'; Path= ' + options.path: ', Options.domain?
'; Domain= ' + options.domain: ',//Do not be confused here, not practical, assuming that the true assignment, will only lead to write failure. Options.secure?
'; Secure ': '].join (')); } var result = key? Undefined: {},cookies = Document.cookie?
Document.cookie.split ('; '): [],i = 0,l = Cookies.length; for (; I < L; i++) {var parts = cookies[i].split (' = '), name = Decode (Parts.shift ()), cookie = parts.join (' = ') ; if (key = = = Name) {result = Read (cookie, value); Break } if (!key && (cookie = read (cookie))!== undefined) {result[name] = cookie; }} return result; }; Config.defaults = {}; $.removecookie = function (key, options) {$.cookie (Key, "", $.extend ({}, Options, {Expires:-1})); return!$.cookie (key); };}));
Examples of how to use the Shopping cart:
function Getscart () {var sc = $.cookie ("S_cart"); Return SC? Json.parse ($.cookie ("S_cart")): null;} function Getscartnum () {var dt = Getscart (); if (dt && Object.prototype.toString.call (dt) = = = ' [Object Array] ' && dt.length > 0) {return dt. Length } else {return 0; }}function addscart (ID, num) {if (!id) return; num = num | | 1; var dt = Getscart (); if (DT) {var isexist = false; if (Object.prototype.toString.call (dt) = = = ' [Object Array] ' && dt.length > 0) {for (var i in DT) { if (dt[i].id = = id) {dt[i].num = num; Isexist = true; }} if (!isexist) {Clearscart (); Dt.push ({"id": ID, "num": num}); }} else {dt = [{"id": ID, "num": num}]; }} else {dt = [{"id": ID, "num": num}]; } $.cookie ("S_cart", json.stringify (DT), {expires:10, path: '/'}); return DT;} function DelscarT (ID) {if (!id) return; var dt = Getscart (); if (DT) {if (Object.prototype.toString.call (dt) = = = ' [Object Array] ' && dt.length > 0) {var IDX =-1; for (var i in dt) {idx = Dt[i].id = = ID?
I:-1; } if (idx >-1) {Dt.splice (I, 1); Clearscart (); $.cookie ("S_cart", json.stringify (DT), {expires:10, path: '/'}); }}}}function Clearscart () {$.cookie ("S_cart", NULL, {path: '/'});}
This is the method of C # background fetching cookies:
var ck = request.cookies["S_cart"]; var SCart = Httputility.urldecode (CK = = null?)"": CK. Value);
C # go to JSON string as an object method:
if (!string. Isnullorwhitespace (S_cart)) { list<s_cartparam> cs = new list<s_cartparam> (); DataContractJsonSerializer serializer = new DataContractJsonSerializer (cs. GetType ()); MemoryStream mstream = new MemoryStream (Encoding.UTF8.GetBytes (S_cart)); CS = Serializer. ReadObject (Mstream) as list<s_cartparam>; }
Some notes on JS operation Cookie