Php shopping cart [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
开发者_开发技巧 Improve this questionWhen we write a shopping cart, should we save cart info in the database with session id or should we save only session variables? Why?
Actually that depend what do you want to do with it.
If you want that the a login user can get his cart 2 days after (or whatever) you should put it in db. But you can't save a no-login user cart (or you save it only to track item). So you have to check every times if the user is log.
The advantage is if the user use another browser or computer, he will still have is cart.
For the databases it must not change anything, cos normally you will have a history (if the user buy the product) so you just had a field history which is a boolean, and have a cron who clean history every month (if you need it).
If the user can't save his shopping cart, the session ID would do fine, just remember that it can change if he starts over the browser.
If you want the user to be able to store and retrieve the cart, you can't rely on the session ID., In this case you need a user account and then tie the cart to the User ID instead.
[EDIT]
First of all, yes I think you should store the cart in a db.
As long the customer don't have a user account, the only way to identify him/her is by the session ID or if you write a cookie on the client identifying him/her.
But in the end, depending on how reliable cart you want, you can't trust the session ID because it can change if the customer restarts the browser. Writing a cookie is not reliable either because the customer might have disabled cookies. Session variables are of course pending on the session ID.
If you wan't 100% reliable cart, you relly need to have the customer to login so you can use a UserID instead of the session ID.
Personally there is no real problem with storing a shopping cart within the user session but obviously a user will lose the contents of their shopping cart once the browser has been closed. So really it depends on whether you believe your customers will benefit from having a shopping cart that is persistent or not.
There is no problem with storing the shopping cart in the database and I Would recommend this as you can then see items that people are adding to cart and then not buying which could indicate some pricing issues
So the short answer .... add it to the database :)
store it in a database. It's not that hard to do, and it will give you the best results. furthermore, it allows you to track items that people put in their baskets but don't purchase.
精彩评论