HTML5 Introductory Tutorial: Cookie Session Tracking Technology ~

Source: Internet
Author: User
Tags set cookie

1. Cookie Session Tracking Technology Introduction

Session tracking is a common technique used in Web programs to keep track of a user's entire session. Common session tracking techniques are cookies and sessions. Cookies can save temporary data on the client by identifying the user in the client record information.

Since the advent of Cookie technology, it has become a major issue for Internet users and web developers. Some network users, even some experienced WEB experts are also dissatisfied with its production and promotion, this is not because the function of cookie technology is too weak or other technical performance reasons, but because the use of cookies is a threat to the privacy of Internet users. Because a Cookie is a small text file that is saved by the WEB server on the user's browser, it contains information about the user.

Cookie technology comes from the rapid development of HTTP protocol on the Internet. With the deep development of the Internet, bandwidth and other restrictions do not exist, people need more complex internet interaction activities, you must maintain active status with the server. Therefore, in the early stage of the browser development, in order to adapt to the needs of users, technology has introduced a variety of Web browsing to maintain the state of the means, including the Cookie technology. In 1993, Netscape employee Lou Montulli invented the widely used Cookie today in order to allow users to further improve their access to a website and to further implement a personalized network.

A Cookie is a way for a server or script to maintain information on a client workstation under the HTTP protocol. A Cookie is a small text file that is saved by the WEB server on the user's browser (client) and can contain information about the user. The Web site can access Cookie information whenever a user is linked to a server.

At present, some cookies are temporary and some are persistent. The temporary cookie is only stored on the browser for a specified period of time, and once the specified time is exceeded, the cookie is purged by the system. Persistent cookies are stored in the user's cookie file and can still be invoked the next time the user returns. Cookies are stored in cookie files, and some users are concerned that the user information in the cookie is stolen by some ulterior motives, causing some damage. In fact, users outside the site cannot cross the site to obtain Cookie information. If you block cookies because of this concern, you will certainly deny access to many site pages. Because there are many Web site developers today who use cookie technology, such as the use of Session objects is inseparable from cookie support.

The technology of session tracking is to maintain the communication information between client and server, and the cookie is a kind of conversation tracking technology.

A cookie is a way in which the server or script can maintain client information under the HTTP protocol;

Cookies are saved in the client, usually saved in the cookie Temp folder of the browser and can be deleted manually;

When the user accesses the server, the server can set and access the cookie information;

Cookies are transmitted using the HTTP header information in the Code of the Web page, and each page request from the browser can be accompanied by a cookie.

2. Mechanism of cookies

In the program, session tracking is a very important thing. Theoretically, all request actions for one user should belong to the same session, and all request actions for another user should belong to another session, and they should not be confused. For example, any item that user a buys in a supermarket should be placed in A's shopping cart, regardless of when user a buys it, it belongs to the same session and cannot be placed in User B or User C's shopping cart, which is not part of the same session.

The Web application transmits data using the HTTP protocol. The HTTP protocol is a stateless protocol. Once the data has been exchanged, the client-to-server connection is closed, and exchanging the data again requires establishing a new connection. This means that the server is unable to track the session from the connection. That is, user a purchases a product into the shopping cart, and when the product is re-purchased, the server is unable to determine whether the purchase is a session of user A or User B. To track this session, you must introduce a mechanism.

Because HTTP is a stateless protocol, the server does not know the identity of the client from the network connection. What do we do? Give the client a pass, one per person, who must bring their own pass for whoever accesses it. This allows the server to confirm the identity of the client from the pass. That's how cookies work.

A cookie is actually a small piece of text information. The client requests the server and, if the server needs to log the user state, uses response to issue a cookie to the client browser. The client browser will save the cookie. When the browser requests the site again, the browser submits the requested URL along with the cookie to the server. The server checks the cookie to identify the user state. The server can also modify the contents of the cookie as needed.

Cookies when you browse the Web, the Web server is placed in a small TXT file on the client End, which is your computer. This file stores some things about the site you visit, and the next time you visit this website, the cookie remembers some of the status or settings of your last visit, allowing the server to send relevant content to the page. The information contained in the cookie does not have a standard format, and the specifications of each Web server may be different, but it will generally include: the domain name of the website visited, the time of the visit, the client's IP address, etc., the visitor's information about the site, and so on. For example, you set up a page like Google to display a few search results and other information, even if you do not sign in to your Google account, you will be able to save the next visit, this is the last time you visited the information into the cookie effect. If it's an online shopping site, it also records information about your shopping cart, storage shelves, and your account name. Other websites use cookies to write down your login ID and password so that you can log in automatically the next time you open your browser.

Of course, if you open the TXT file of the cookie in the System folder, you will not see this information and only see a bunch of messy characters, because for the sake of security, the content of the cookie is generally encrypted and only the corresponding server can read it. Also, because cookies are just txt files, not programs, not viruses, run on their own, have no impact on the operating system and any other computer programs, and do not spread over the internet, it does not actually pose a threat to Internet security.

For web analytics, cookies are designed to help web analytics tools that embed code classes record information about the site's access (Visit) and visitors (Unique Visitor), which cannot be monitored without a cookie. Software that uses server-side log for website analysis does not require cookies to be able to perform correlation analysis, so cookies are only valid for embedded code class tools. The tools you're familiar with--google Analytics, Omniture, HBX, WebTrends (embed code), and so on, require cookies to be placed on the site visitor's computer for monitoring.

3.Cookie How to use

In the Java language, we use Document.cookie: Get/Set Cookie

The format is: Name=value; [expires= expiry time]; [path= access Path]; [domain= domain name]; [Secure]

such as: Document.cookie = "Username=zhangsan";

Application Scenarios for 4.Cookie

4.1 Automatic Login

4.2 E-commerce shopping cart function

4.3 Record the number of user login URLs

4.4 Item Browsing History

5. E-commerce shopping cart case

The shopping cart function is implemented locally in the browser.

In the Product Details screen, we can click the Add to Cart button, the item added to the shopping cart, the product data added to the shopping cart is actually stored in the cookie, the Product details page such as:

After adding multiple items to the shopping cart, we can go to my shopping cart to view the items in the shopping cart, which we have obtained from cookies, such as:

Shopping Cart Merchandise Display after we can continue to the shopping cart in the operation of each item, such as the number of additions and deletions, product deletion, price statistics, batch deletion, select all operations, such as the product can be deleted, at the same time we will delete the data in the cookie to synchronize changes.

Source: Qian Feng HTML5

HTML5 Introductory Tutorial: Cookie Session Tracking Technology ~

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.