Store shopping cart on customer or server?

Asked

Viewed 1,234 times

1

What better way to store items from a shopping cart?

In itself javascript, on the client side or in a session on the server, either using $_SESSION(php) or Session["sessao"](C#)

Which would be better? safer and better usual for the user?

  • What programming language are you using? PHP or C#?

2 answers

8


In session or in a cookie.

If you saved in Javascript, first it won’t work if the user has disabled Javascript, second if the browser or the computer "crashes" it loses its entire cart (you can mitigate this by using localStorage - when supported).

If you use the session, it depends: what happens if the user closes the browser (intentionally or not - even if above)? If all data is deleted at the end of the session, then there is the same problem. And performance can be worse if you need to access the database each time the user includes a new item.

Usability

A common problem with shopping carts is what happens when the user opens more than one tab at a time, or uses the "Back" button - in such cases, what is appearing on the screen is one thing, what really is in the cart may be something else. Personally, I don’t know what the user’s expectation will be: a) that the cart looks exactly the same as it does on the screen; or: b) that what he did on one tab is "safe" in some way, even when he moves the other. How you implement has an impact on one or another scenario.

The best I can suggest in this case is to include a random token in the submission forms (for example, as a Hidden input) and always compare this token to that of the cart before performing an action. If they are different, show the same page again for the user, updated - so that he is seeing the correct cart before the action is actually done.

Security

If you are using https as I hope it is, it doesn’t make much difference how it’s stored. You can sign the cookie data on the server if you want to prevent the client from tampering with the values inappropriately (including, this is how many frameworks do to keep session data in a cookie - and not in the BD - without compromising site security)but that may not even be necessary.

  • Very good his explanation @mgibsonbr, only in relation to cookies, if he tried to make a purchase and closed the site, it is because he did not want at that time, so I see no reason for him to leave saved his last purchase attempt, besides that would have to expire this cookie after X time. I think the session is a very valid way, but I could use anonymity object not ? to not always have to look for that product in the bank, what do you think? In the most cool your answer

  • @Rod Yeah, I don’t know how to answer what users would expect: keep saved or erase everything. I think that each one expects something different from the sites/ programs... As to use "anonymous object", I’m sorry but I don’t know what it is. Talk about something in cache, in memory?

5

On the server.

One of the reasons would be that the variable Session evaporates when the user leaves the site or closes the browser. In this case, it would be interesting for the site to "remember" the last purchase attempt made by the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.