Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP run a script when a session expires

I have a cart application with unique items with qty 1 - I need to deplete the stock qty when a user puts one in the basket to avoid someone else buying it. Problem is, that if they abandon the cart without buying I need to put the item back in stock.

Is there a way I can replenish the stock when the cart session expires? ie run a script to replace the stock.

like image 837
jim smith Avatar asked Dec 07 '11 13:12

jim smith


2 Answers

You can use session_set_save_handler to create a custom session-handling class. When you do this, you can decide other actions that need to run either when the garbage collector is called or at session destroy. If you decide to work with the garbage collector, ensure that you also know the values for session.gc_divisor and session.gc_probability and also understand what they do (these set the probability that the garbage collector will run).

like image 85
mishu Avatar answered Oct 13 '22 03:10

mishu


To avoid the need of cron jobs; keep it simple:

  1. A user picks places a object in his/hers basket
  2. Timestamp for "placing object in basket" is stored in the database, and a timeout timestamp is stored too. For example (time() + (60*20))
  3. User closes webpage
  4. On every pageload you do a simple mysql-query to check if timeout-timestamps are smaller than the current timestamp. If the query returns a result, delete from basket and put back in slot.

(This require that you update some sort of "last activity" in the database too)

Should be pretty straight forward.

like image 33
OptimusCrime Avatar answered Oct 13 '22 03:10

OptimusCrime