I'm trying to figure out the best way to handle a lock-while-editing problem.
I'm using a session variable to hold a logged in user's username. if a user selects a record for editing, my script first checks if there's a username in the field called "locked". If there is, and it's not the current user, the script won't let them edit. If there's nothing in "locked" it writes their username to that field and lets them edit. If their username is already in the field it lets them edit. If they click "Submit" or hit a link to return to a main list of records, the "locked" field is cleared - so the record is available for editing once more by any logged in user.
There are two problems. First, a user can click on a nav bar link to go to a different page, or select a bookmark, type in a URL, etc. In which case the record stays locked unless they come back, edit it again, and Submit or return to the list.
Second, a user can close their browser, quit or crash. Once again the record stays locked.
I'm thinking... for the first case, maybe some kind of javascript thing - like those annoying popup-on-exit windows except instead of a window, a chunk of PHP is sent to MySQL to unlock any locked records.
For the second case - maybe when I write the username to the "locked" field, I also get the date/time from the server and write it into another field. Then I have my script check to see if there's a time difference of, say 15 minutes or more. If there is, it bypasses the lockout and lets someone edit, even though they have a different username than what's in "locked". At the same time, I could set a session timeout (I think I can do this - right?) of say 14 minutes so if the user just walked away from their desk, they get disconnected - this way someone else can get into a record if they just wait a little while.
What do you think? Worth trying? Better ways to accomplish all this? I'm brand-new to the whole authentication/session thing, so any advice will be greatly appreciated!