Search code examples
phpabort

Ensuring that script exit on user abort


I wrote a script that has no time limit and max_execution_time is 0

  1. I should work until the end of it's job or on User abort (canceling from browser).
    How can I be sure when user aborts page load from browser the script is killed by server?

  2. Does this kills script immediately or __destructor of classes are called before?


Solution

  • Assuming this is called by ajax, you could add a event handler for when the user leaves the webpage that sends an ajax request that is handled by a script that kills the other script.

    This could be implemented by having the script create a file when it starts. Then, every one and a while it checks to see if that file is still there. If it is it continues if not it quits.

    The trick is finding the filename. The way I do this is give every page request a unique user ID. And both the 'worker' script and the 'kill' script are called with the same user ID in the url (GET).