PHP runs the script when the session expires

I have an application for a basket with unique elements with a quantity of 1 - I need to deplete the stock when the user puts it in the basket so that someone else does not buy it. The problem is that if they leave the basket without a purchase, I need to return the goods to the warehouse.

Is there a way I can replenish my stock when the trolley session ends? those. run the script to replace the stock.

+5
source share
3 answers

You can use session_set_save_handlerto create a custom session processing class. When you do this, you can decide other actions that must be performed either when the garbage collector is called, or when the session is destroyed. If you decide to work with the garbage collector, make sure that you also know the values ​​for session.gc_divisorand session.gc_probability, and also understand what they do (this sets the probability that the garbage collector will work).

+4
source

To avoid the need for cron; keep it simple:

  • The user selects an item in his cart.
  • The timestamp for "placing the object in the basket" is stored in the database, and the timeout timestamp is also stored. for instance(time() + (60*20))
  • User closes webpage
  • mysql-, , , . , .

( , - " " )

.

+1

, sessions. session_id DATETIME TIME last_activity.

, carts, (, product_id quantity) session_id. 2 :

  • , "" carts, .
  • The user leaves the basket, a cronjob is called, which checks if the value last_activityis greater than what you want it to be (say, more than an hour ago). Have the script re-stock your stock with the amount that was in the basket.

Note that you will need to update the field last_activityin your bootloader / bootloader (the mechanism that runs on each loaded page).

0
source

All Articles