I am new to PHP, which I used to develop a small web-app. In my design I counted on a feature I originally thought to be easy to employ but the further I get into the more I realize it's probably a rather fundamental problem.
The setting is following:
I have blocks of data, let's call them data_1,...,data_n
for any i, data_i may be not accessible
when a user connects, he gets the first available bulk of data and marks it as inaccessible
when the user stops working, the data are made accessible again
there is no login, a session id is used for the user identification
the data are stored in a MySQL database
Now, I have implemented it in such a way that on the request, the user locks the table with data, finds the first available, sets a mark and unlocks the table.
The problem is when the user does not remove the mark - I need to make the data available again at some point by default.
I tried JS, but Window.onbeforeunload is not guaranteed to run (e.g. on crash).
I tried running a PHP script that would poll the connection. But I need to execute other scrips in between and I got to understand that this is not possible because only one script can access session data at the time and therefore the polling script block execution of others.
I have resolved the matter by saving timestamps with each communication with the data the user has exclusive access to (and adding script that makes connections while the windows is open to prevent timeout). If new user comes, he checks the timestamps and if there is long enough pause, he takes the data and stores his timestamp.
My solution however is a workaround, does not really answer the problem. Googling shows that everyone is asking about the problem of how to keep execution after connection is lost - but that's not purely this case because for me there is no execution by the time the connection is lost.
The question is: How, using PHP 5.4, can I create a script that executes after the connection is lost?
Thanks.