Reputation: 1748
I would like to know what the best way to save a shopping cart is. I'd like the shopping cart details (which are session variables) to persist even when the user closes his browser. I would consider saving the data in a table as soon as the window close event is fired but i'm not sure that jquery unLoad or beforeUnload events are what i need as they don't seem to work across different browers.
I'd appreciate any pointers to set me on the right path
Upvotes: 0
Views: 3611
Reputation: 535
jQuery unLoad method works everywhere. I assume you want to store the cart in DB, when you talk about table(s). So you have to make synchronous ajax call to your server, so PHP could store it and probably return an identifier, which you should put into an cookie.
Problem is that this procedure can take relatively long time (1-2s). I'd just provide a Save button.
Upvotes: 0
Reputation: 9456
You just save the data as soon as the data change is made. Why wait? As soon as someone adds an item to their cart, save it.
Upvotes: 0
Reputation: 2505
It's not so much when a browser closes as keeping the session cookie in the browser. If I understand correctly, you're using sessions (i.e. the $_SESSION variable), so it should be relatively easy. PHP's function session_set_cookie_params would most likely be the way to go; there's also the option session.cookie_lifetime
(found here) that explains the session's cookie lifetime a bit more, even if you can't set it yourself.
The session cookie's lifetime is in seconds; so if you set it to 60 seconds and visit the site, the session cookie will only last 60 seconds before discarding the cookie, effectively destroying the session. Set the lifetime to a high number in order to prevent this.
Upvotes: 4
Reputation: 6359
Why not just keep your shopping cart data persisted anyway. If you want it when they're on the site and you still require it when they've left, just persist it. It's tricky to reliably pick up when a session is abandoned as they could close the machine down or kill the browser process. Every change to the cart should be saved until they explicitly kill their cart or the cookie associated with it.
Upvotes: 0
Reputation: 10992
Maybe save data when they are changing and not when the browser is closing. Saving could be in Database, Local Storage or Cookies.
Upvotes: 1
Reputation: 324620
Since the session data is server-side, and all the browser has is a session ID, all you need to do is make the session ID last longer.
This is done by editing the php.ini
settings, particularly those regarding the lifetime of the session cookie. If it's 0
, then the cookie is cleared when the browser closes.
Try setting it to a high number. That will make the session persist.
Upvotes: 1