The difference between a cookie and a session

Source: Internet
Author: User

Cookies (Small cookie)

1.Cookie is a technology that allows a Web server to store a small amount of data (4KB or so) on the client's hard disk or memory and read it out.

2. When you browse a website, a very small text file placed on your hard drive by the Web server, it can record your user ID, the pages visited or the time of stay, etc. Herod want you to save the information. When you visit the website again through a browser, the browser will automatically send the cookie that belongs to the website to the server, and the server will make the corresponding action by reading the cookie to know about your information. If you display a welcome banner on a webpage, or if you don't have to enter an ID, you can log in directly and so on.

3. When a browser accesses a site, only the cookie belonging to the current site is sent to the server,

4. The browser saves cookies in two ways: 4-1 in the memory of the browser, and 4-2 on the hard drive of the computer on which the browser resides. The cookie object for the browser that is not going to be written sets the expiration time; Expires=datetime.now.addminute (5);//Set Cook to fail after 5 minutes development scenario: Commonly used to log in and save user recently viewed items

5.Cookie is the Key-value collection Response.appendcookie (New HttpCookie ("Laoma", "Ox Man") from the server-side written to the browser to get the cookie value from the customer service side. Response.cookie[i].value; On the server side cannot directly operate the cookie on the browser, can set the cookie expiration time in the browser side to let the browser to empty the cookie int cookiecount=this. Request.Cookie.Count;

for (int i=0;i<cookiecount;i++)

{

Create a new cookie, but the name is more requested than the same HttpCookie cookie=new HttpCookie (Reques.cookie[i]. Name); Set the expiration time cookie. Expires=datetime.now.addhours (-1);

RESPONSE.COOKIES.ADD (cookie);

}

The domain in the Cookie if the current page is a sub-page under Baidu, set the cookie, if you want to let Baidu this main page can also access the cookie in the subdomain, the domain is set as the main field cookie.domain= "www.baidu.com";

6. If you set the expiration time browser to save the cookie to the hard disk, if not set the expiration time to save in memory

7. The disadvantage of 7.1Cookie is the same as the form, and cannot store too much information,

7.2. When the website requests jpg,js.css This kind of file, also carries the cookie, this reduces the website performance can put the dynamic file static file in the different domain, the cookie cannot boast the domain, thus can achieve the website optimization

Seeion

1.Session provides a way to keep information in server memory (it can store any data type including custom objects)

2. The session of each client is isolated storage.

3.Session objects are used to store information about customers

4. This information will be retained throughout the user session (before the cookie that saved the SessionID is lost)

5. The variables stored in the session object are not discarded when the current user is browsing from one Web page to another in the application.

6.Session can only be accessed by the user of the session (because SessionID's cookies are stored in the cache of the visitor's browser)

7. User cannot access and modify the session of others 8. When the current session expires or terminates, the server clears the Session object app: Commonly used to save login user ID

The difference between a cookie and a session

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.