Shopping Cart principle of?

Cart is equivalent to the reality of supermarket shopping cart, except that the entity is a car, a car is only virtual. Users can page between the different shopping sites Jump to buy their favorite products, when clicked purchase, this product will automatically save to your shopping cart, after repeat purchase, and finally selected on all goods unified shopping cart to checkout checkout, and this is as far as possible to allow customers to experience the real life shopping feeling. Action by the server to track each user, in order to ensure at checkout Every item has its main object.

The key to the shopping cart that server recognizes each user and maintain contact with them. But the HTTP protocol is a "non-state (Stateless)" agreement, which the server can not remember who buy goods when the goods into the shopping cart, the cart server does not know what some of the original, so that users in different when the cart can not jump between pages "carry" to achieve this gave the cart caused some difficulties.

Currently cart achieved mainly way cookie, session or in conjunction with the database through. Analyze the following mechanisms and their role.

cookie

A cookie is generated by the server, a piece of information stored in the client. It defines the client-side storage mechanism and return information in a Web server, cookie file that contains the content domain, path, lifetime, and tag values ​​set by the server. When users visit the same Web server in the future, the browser cookie will as it is sent to the server. By reading the previously saved to the server to the client information, the site provides a series of convenience for visitors, such as online transaction process to identify the user identity, security less demanding situations to avoid duplicate user name and password to enter the home page, portal customized, targeted advertising and so on. Take advantage of the characteristics of the cookie, greatly extends the functionality of WEB applications, not only can establish contacts the server and the client, because a cookie by the server custom, it is also possible to Shopping Information generated cookie value is stored in the client, enabling the shopping cart Features. With the realization between the server and browser session or cookie-based shopping cart the way, it has the following characteristics:

1, cookie stored on the client, and takes up very little resources, browsers allow cookie store 300, the size of each cookie is 4KB, sufficient to meet the requirements of a shopping cart, but also reduce the load on the server;
2, for the cookie the built-in browser, easy to use. Even if the user accidentally close the browser window, as long as the cookie is valid definition of information in the shopping cart will not be lost;
3, cookie is not an executable file, so it will not be performed in any way, and therefore will not bring virus or attack the user's system;
4, cookie-based shopping cart requires the user's browser must support and enable cookie set, otherwise the cart is invalid;
5, there is debate about the cookie privacy violations visitors, so some users prohibit cookie functions of this machine.

 

session

session is another way to achieve the shopping cart. session offers can be saved and track the status information of the user's function, so that the current user can be shared between page variables and objects defined in session, but can not be accessed by other applications, users, and it's the most important difference is that cookie , session will be stored in the user's private information during a session on the server side, it improves security. After generating the session server, the client generates a sessionid identification number stored in the client, and in order to maintain synchronization server. The sessionid is read-only function if the client ban cookie, session will be transmitted by other means between pages in the URL additional parameters, etc. or implied submit the form. Therefore, the use of implementation and management of the user's session is safer and more effective.

Also, with the session can be achieved cart, the characteristics of this approach are:

1, session keeping with a new mechanism to synchronize with the client, not dependent on the client settings;
2, compared with the cookie, session information is stored on the server side, so it is more secure, and therefore may be identity marked, shopping and other information stored in the session;
3, session consumes server resources and increase the load on the server side, especially when many concurrent users, it generates a lot of session, affect the performance of the server;
4, because it is more sensitive information stored in the session, and is based on files saved in the server, so there are still a security risk.

Combined with the way the database

This is also more common model, in this way, the database assume the role of store shopping information, session or cookie is used to track users. In this way it has the following characteristics:

1, the database and cookie are responsible for recording and maintaining data session, can play to their strengths, so that security and server performance have been improved;
2, every shopping behavior, we must establish a direct connection to the database until the table after the operation is complete, the connection is not released. When many concurrent users, will affect the performance of the database, therefore, put forward higher requirements on the performance of the database;
3, to maintain the cookie session depends on the support of the client.

The choice of a variety of ways:

Although the cookie can be used to achieve the cart, but must be supported by the browser, plus it is stored in the client information can easily be acquired, so this also limits it to store more and more important information. Therefore, the general session cookie is only used to maintain the server, for example, the nation's largest online bookstore Dangdang is to use cookie to keep in touch with customers, but the biggest disadvantage of this approach is that if the client does not support the cookie will make the shopping cart fail.

Session well to keep the session with both parties, the client may ignore the setting. It has been widely used in shopping cart technology. But the session file attributes so that it still remains a security risk.

Although the database is incorporated to some extent in solving the problems described above, but can be seen in the above example: it involves frequent operation of the database table in such shopping process, each user to purchase a particular product, must connect to the database when the user a lot of time on the increased load on the server and database.

Guess you like

Origin www.cnblogs.com/bluealine/p/11039457.html