How to check when user's session expires when user isn't on the site anymore?
To be more specific, if a customer adds items to their shopping cart, the number of items he/she added are deducted from the quantity available in the DB. But, what if they leave the site altogether? I've been advised to add a timestamp to this user's session, and update the timestamp with every request. When they leave the site, there will be no more updated requests. (Selected items are stored in the user's session)
In PHP, how would I check that particular user's session to see h开发者_开发问答ow much time has elapsed since their last request? My first thought was to store the user's session ID, timestamp, and quantity of each item into the DB, and cron a script to check the elapsed time, and process accordingly, but this would essentially turn my cart into a db-driven cart.
Is there a different or better method of doing this?
EDIT: I apologize for my indecisiveness. I ultimately ended up using Marc B's answer, although Aatch's answer would have accomplished this for me as well. However, I found that I can access the directory where the sessions are saved on the server, and use a cron job to check the timestamps saved in these session files every minute, which is a smoother implementation given my current cart solution.
If you're on standard file-based PHP sessions, you could disable the built-in session garbage collector and roll your own. A simple cron-based shell script could scan the session file directory for any sessions that have no been changed (or touched) in a certain amount of time
Given that list of untouched/potentially expired sessions, you'd open up each on in turn (it's just a serialize()
representation of the $_SESSION superglobal), look for a shopping cart, and fix up your database quantities based on what you find.
I've actually built a system very similar to this. Unfortunately, since you need some form of server side initiative, a cron is really your only way to go in PHP.
As for the cart being a DB driven cart, given that you're already storing all the information in the database, you might as well add the carts and pending information to the database. That was you can take advantage of the DBs transactioning system for safety when running the queries.
I would check how many you physically have in stock and then deduct that number from the number of those items in user's carts who have logged in the past 6 hours (or whatever timeframe you have chosen). If you have a single integer column that keeps track of how many are in stock and remove one when somebody puts that product in their cart that could be a little misleading and hard to organize.
In my opinion, you should be deducting the quantity after they have purchased the item because if one person adds an item to their cart and there is only one left, the next person that comes along will see that there are no more in stock and leave, not knowing that there actually is one left. It should be on a first come first serve basis.
精彩评论