0

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.

Adam Streck
  • 340
  • 2
  • 15

2 Answers2

0
  1. you need to use sockets.

2.look at "Example #1 A ignore_user_abort() example"

Moshe Zino
  • 311
  • 1
  • 4
  • Sorry but I do not see/understand how do sockets help in this situation? Moreover, the exact example you gave I've tried and encountered the problem with blocking of parallel scripts that I've stated above. Could you please elaborate on how to overcome the problem? – Adam Streck Oct 17 '13 at 14:31
  • WebSockets allow bidirectional communication between server and client (the server can ask the client if the block of data can be made accessible or not) while HTTP does not (hence the need for the timeout based workaround). http://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php – juanrpozo Mar 01 '14 at 21:00
0

You can do it in this way.

  1. Related you session_ids & data your are locking for that user who have that seesion_id.

2.When a user starts on your webpage the you can insert his/her session id (session_id()) in database with current timestamp.

On every 20 minutes(using a cron job) or on every page request check your database table for the list of seesion ids active in last 20 minutes.

Fetch that list & keep lock for that session ids and unlock the associated tables for the rest of session_ids.

Peeyush
  • 4,728
  • 16
  • 64
  • 92
  • Thanks for the suggestion, but as I've stated above (please see 7th,8th paragraph), this is a workaround - the question remains. – Adam Streck Oct 17 '13 at 16:45