Implementation principle of shopping cart

Source: Internet
Author: User

A shopping cart is equivalent to a supermarket shopping cart in reality. The difference is that a physical vehicle and a virtual car. Users can jump between different pages of the shopping website to buy their favorite items. When you click buy, the item is automatically saved to your shopping cart, finally, all the selected items are placed in the shopping cart and settled at the payment desk. This is to try to make the customer experience shopping in real life. The server tracks the actions of each user to ensure that each item has its own ownership at checkout.

The functions of a shopping cart include:

N add the item to the shopping cart, that is, order

N Delete the ordered items in the shopping cart

N modify the order quantity of a book in the shopping cart.

N clear the shopping cart

N display the list, quantity, and price of items in the shopping cart.

The key to implementing a shopping cart is that the server identifies every user and maintains contact with them. However, HTTP is a "stateless" protocol, so the server cannot remember who is buying the product. when the product is added to the shopping cart, the server does not know what was originally in the shopping cart, which makes it impossible for the user to "carry with him" when jumping between pages, which makes it difficult to implement the shopping cart.

Currently, the shopping cart is implemented through cookies, sessions, or databases. The following describes their mechanisms and functions.

1. Cookie

Cookie is a piece of information that is generated by the server and stored on the client. It defines a mechanism for the Web server to store and return information on the client. The cookie file contains the domain, path, lifetime, and variable values set by the server. When a user accesses the same web server in the future, the browser will send the cookie to the server as it is. By allowing the server to read the information originally stored on the client, the website can provide a series of convenience for viewers, for example, when a user is identified during an online transaction and has low security requirements, users are not allowed to repeatedly enter their names and passwords, customize the portal's home page, and place advertisements in a targeted manner. By leveraging the features of cookies, the Web application functions are greatly extended, which not only allows you to establish connections between servers and clients, because cookies can be customized by servers, therefore, you can store the cookie generated by shopping information on the client to implement the shopping cart function. Cookie-based session or shopping cart between the server and the browser has the following features:

N cookies are stored on the client and consume a small amount of resources. The browser can store 300 cookies. Each cookie is 4 kb in size, which is sufficient to meet the requirements of the shopping cart and reduces the load on the server;

N cookies are built in the browser for ease of use. Even if you accidentally close the browser window, the information in the shopping cart will not be lost as long as it is within the validity period defined by the cookie;

N cookie is not an executable file, so it will not be executed in any way, so it will not bring viruses or attack users' systems;

N cookie-based shopping cart requires the user's browser to support and set to enable the cookie, otherwise the shopping cart will become invalid;

N there is a debate about Cookie infringing the privacy of visitors, so some users will disable the cookie function of the local machine.

2. Session

Session is another way to implement shopping cart. Session provides the ability to save and track user status information so that the variables and objects defined by the current user in the session can be shared between pages, however, it cannot be accessed by other users in the application. The most important difference between session and cookie is that session stores the private information of the user during the session on the server, improving the security. After the server generates a session, the client generates a sessionid identification number and stores it on the client to maintain synchronization with the server. This sessionid is read-only. If the cookie function is disabled on the client, the session will be transferred between pages by adding parameters to the URL or implicitly submitting in the form. Therefore, session implementation is more secure and effective for user management.

Similarly, the use of session can also implement the shopping cart, which is characterized:

N sessions use a new mechanism to maintain synchronization with the client and do not rely on client settings;

N compared with cookies, session is stored on the server, so it is more secure. Therefore, you can store information such as identity tags and shopping in sessions;

N sessions will occupy server resources and increase server load, especially when many concurrent users generate a large number of sessions, affecting server performance;

N because session information is more sensitive and stored on the server as files, there are still security risks.

3. Database Integration

This is also a common mode. In this mode, the database is responsible for storing shopping information, and session or cookie is used to track users. This method has the following features:

N databases and cookies are respectively responsible for recording data and maintaining sessions. They can take advantage of their respective advantages to improve security and server performance;

N each shopping behavior must be connected directly to the database until the table operation is complete. When there are many concurrent users, the database performance will be affected. Therefore, this puts forward higher requirements on the database performance;

N. The client supports cookie persistence.

Various options:

Although cookies can be used to implement shopping cart, they must be supported by browsers. In addition, they are stored on the client and can be easily obtained. Therefore, this limits the storage of more information, more important information. Therefore, cookies are generally used to maintain sessions with servers. For example, the largest Dangdang online bookstore in China uses cookies to maintain contact with customers, however, the biggest disadvantage of this method is that if the client does not support cookies, the shopping cart will become invalid.

Session can maintain a session with both parties, and the client settings can be ignored. It has been widely used in shopping cart technology. However, the file attributes of the session leave security risks.

Although the database-based approach solves the above problems to a certain extent, the preceding example shows that frequent operations on database tables are involved in this shopping process, in particular, every time a user buys a product, they need to connect to the database. When many users, the load on servers and databases is increased.

 

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.