Reputation: 40497
I have implemented simple shopping cart in a website. I save the cart and items in Session
object. After user checkouts (using Paypal), I clear the cart items. This works fine, but I have seen a problem in following scenario:
Suppose a user added some items to cart and opened another browser and logged in. He can now see the items in both browsers. Now if he checks out in one browser he is still able to see the cart items in other window as both browser have separate sessions.
What approach should I adopt to avoid this or will have to live with this?
EDIT:- After posting the question I was thinking about it. I will settle for this simple solution, Whenever user goes for checkout, I will hit the database to load the cart instead of session. This way I won't be hitting database for showing items in cart (on top) and there won't be any checkouts based on phantom items.
Upvotes: 0
Views: 1520
Reputation: 26737
Your web app is based on HTTP protocol which open and close a connection just to satisfy an Http Web request. Now it is quite normal that if you open another two browser and check out on one of them you can still see the same page (infomration) on the other one. What I think you should do is just avoid that a user can check out twice or handle if someone is trying to check out an empty basket(which is your case if the user hit checkout on the second browser) and it that case you can just show a message. If you go for the solution for which you should refresh at interval the page bear in mind what it could happen if the user open and log in into 250 different sessions: how many time your web app will be hit? If you go for my suggestion all user session will get just an error page like: The bastek is empty. I hope it makes sense
Upvotes: 0
Reputation: 66641
From the moment that you save the cart items on the session and you have diferent sessions you have diferent carts.
To eliminate this, you need to have a common place for all session that you going to save your cart, and this is a table on a database. This common place are connected to the user ether with the user id, ether with the user cookie.
Upvotes: 1
Reputation: 13275
You'd have to AJAX-ify the shopping cart panel section of the page and use setTimeout()
to refresh it at regular intervals.
Probably more effort than it's worth, however - if you're doing it right, clicking 'checkout' again on the 2nd page shouldn't cause duplicate transactions and the list will refresh on the next page load.
EDIT
And by "doing it right" I mean tracking cart items based on the user's Id, not just the Session object.
Upvotes: 0