0

I am developing a Shopping Cart using PHP and stored the data in session. I plan to stored the data in session into database when the user logging out. So when they log in back, I can pull the Shopping Cart data from database.

But with this approach if the user has close browser without logged out and the session has expired, the data will lost without having stored in database.

Is there any way to handle the problem ? without having to directly stored to database each time user add an item to the Cart.

Remy
  • 11
  • 4
  • 1
    You can use cookies to save the necessary information on the user's machine. – hungrykoala Dec 15 '17 at 07:42
  • Does this answer your question? [create cookies with session php?](https://stackoverflow.com/questions/35645244/create-cookies-with-session-php) – TylerH Apr 25 '22 at 18:29
  • See also https://stackoverflow.com/questions/17769011/how-does-cookie-based-authentication-work – TylerH Apr 25 '22 at 18:30

2 Answers2

0

you can confirm user before closing the browser or tab. maybe this article help you. Confirmation before closing of tab/browser

Umar Majeed
  • 313
  • 3
  • 15
0

Save the session on login and not when logged out it is better to do that on logged in. when the user does something you will always save the state. then the user can be logged out or logged in but the session is always updated

you can also keep the data in local storage

nice example : http://phppot.com/php/simple-php-shopping-cart/

Tuz
  • 1,810
  • 5
  • 29
  • 58