cookie parsing

Reference: https://www.runoob.com/js/js-cookies.html

Cookie is some of the data stored in a text file on your computer in to access records stored in the user .

When the web server sends the web page to the browser after the connection is closed, the server does not record user information.

Cookie role is to solve the "how to record the client's user information":

  • When a user accesses a web page, his name can be recorded in a cookie.
  • When a user accesses the page, you can read the user access records in a cookie.

1, storage: key-value pairs

  E.g:

username=John Doe

When a browser requests a web page from the server, belonging cookie of the page will be added to the request in. Services end users to obtain information in this way.

By default, cookie deleted when the browser is closed

2, JavaScript add Cookie

  Join cookie content

document.cookie="username=John Doe";

  Join Cookie expiration time, once expired, the next time you need to re-enter the forms are stored cookie

document.cookie="username=John Doe; expires=Thu, 18 Dec 2043 12:00:00 GMT";

  Join using cookie page

= the document.cookie " username = John Doe; Expires = Thu, 18 is On Dec 2043 12:00:00 GMT; path = / " ; # used at this time is the GMT time format, UTC may be

  When the need to input a plurality of key-value pair in the cookie, the use of ";" separated.

3, read cookie

document.cookie;

  will document.cookie string returns manner all the cookie, the type of format: cookie1 = value; cookie2 = value ; cookie3 = value;

4, modify the cookie

document.cookie="username=John Smith; expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";

  Direct overwrite the old values, complete the changes.

5, delete the cookie

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

  Set the time expires before the cut-off time has expired, cookie deletion. Not necessary to specify the value of the cookie

Example:

the setCookie function (CNAME, cValue, exdays) 
{ 
  var D = new new a Date (); # Get the current time of the object 
  d.setTime (d.getTime () + (exdays * 24 * 60 * 60 * 1000 )); # d.getTime () Get the current time, exdays * 24 * 60 * 1000 milliseconds conversion value, then the new time is set to the time of d
   var Expires = " Expires = " + d.toGMTString (); # d.toGMTString display in GMT in the form of output time. 
  the document.cookie = CNAME + " = " + cValue + " ; " + Expires; 
}

  cname is the name of the key, cvalue is a value corresponding to the key. exdays means that the cookie survive a few days.

Guess you like

Origin www.cnblogs.com/smartmsl/p/10958817.html