I’m working on a project that involves multiple users and the ability to alter data.
Basically, when a user land on a page, he can enter information for a particular entry in a database. While he’s on this page, no other user can access that particular entry. When he finishes, the entry becomes open again. Now, to restrict access to the entry is easy. I set it up so when the particular entry is selected, a value in the db states it is “Inactive” and no one else can get to that page. On the page itself, there’s a “leave” and a “submit” button. Either of these will set the entry back to active.
The trouble I have is if the user decides to click on a different link, close the tab or somehow navigate away. How can I structure it to restore the entry back to an active state? I was looking into the “onunload” event and potentially using it to make an AJAX call. Is this the most logical route to take or is there something similar I’m missing due to my limited knowledge? Thanks for all your help.
I wouldn’t go the
onunloadway (at least not exclusively), as it’s not reliable in case of crash, power loss, etc. Chances are that entries could be locked “forever” in such case.I’d embed an Ajax method in the page, which periodically requests a PHP script to “confirm” that the page is still alive.
You can use such “confirmation” requests to build/update a table to track current locks, working with s/t like a
lock_idwhich uniquely identifies the “entry” being locked,session_idto uniquely identify the browser session holding that lock andexpire_timestampto set the time at which “entry” should be unlocked again in case no more “confirmation” requests ofsession_idcome in and raise itsexpire_timestamp.In combination with a cron job, periodically deleting records having exceeded their
expire_timestamp, that should be a more reliably way to do what you are trying to achieve.