Js cookie and session

Source: Internet
Author: User

 

Cookie concept: stores a string attribute on the client, and obtains a string when reading the cookie value. Besides the name and value, there are also expires expiration time, path, domain, and secure security.

The Cookie of JS Code is worth saving: cookie is a string attribute of the document. To save the cookie, you only need to create a string in the format of name = <value> (name = value), and then set the document. cookie of the document to be equal to it. Example:

Document. cookie = "username" + username;

The Cookie of JS Code is worth reading:

// Define the cookie for storing Variables

Var strCookie = document. cookie;

// Cut multiple cookies into multiple name/value pairs

Var arrCookie = strCookie. split (";");

// Traverse the cookie array to process each cookie pair

For (var I = 0; I <arrCookie. length; I ++ ){

Var arr = arrCookie [I]. split ("= ");

// Find the cookie named userId and return its value

If ("userId" = arr [0]) {

User = arr [1];

Break;

}

}

Conclusion: keep cookies simple. Do not rely on the existence of cookies. Do not store too much information in each cookie. Do not save too many cookes. However, in the hands of skilled WEB administrators, the concept of cookie is a useful tool.

 

Session concept: string attributes stored on the server;

Session value storage: session. setAttribute ("name", name );

Session worth reading:

String num = (String) session. getAttribute ("name ");

// Returns the Object, which can be forcibly modeled as a string;

From song lixing's column

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.