php - Unlock table if browser is closed

44

If someone wants to edit an entry in a table, I put a column "Locked" to true. If the user closed the browser / tab, how can I do it best so that the value is written by "Locked" back to false?

Is that possible with ajax? This can be seen when the user closes the browser / tab, then that ajax event fired?

Or is there a better and more reliable method? I work with laravel 4 und MSSQL.

790

Answer

Solution:

This is very dangerous and highly unreliable, so if you need those rows unlocked, you better put a timeout on them too (a locked_at column), index the column and search for locked rows to unlock the timeouted ones. But...

You can hook on browser unload javascript event to help you with that:

window.onbeforeunload = function(event) {
    var s;
    event = event || window.event;
    if (requestsPending > 0) {
        s = "Your most recent changes are still being saved. " +
            "If you close the window now, they may not be saved.";
        event.returnValue = s;
        return s;
    }
}
405

Answer

Solution:

Tricky to do reliably, a couple of methods:

Use onbeforeunload as Antonio suggests, you could do an ajax request to release the lock?

Lock the record for x minutes on edit, then renew the lock via a polling ajax request just before the lock expires.

People are also looking for solutions to the problem: php - How to change date language?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.