I want to run a relatively time consuming script based on some form input, but I'd rather not resort to cron, so I'm wondering if a php page requested through ajax will continue to execute until completion or if it will halt if the user leaves the page.
It doesn't actually output to the browser until a json_encode at the end of the file, so would everything before that still execute?
It depends.
From http://us3.php.net/manual/en/features.connection-handling.php:
That would seem to say the answer to your question is "Yes, the script will terminate if the user leaves the page".
However realize that depending on the backend SAPI being used (eg,
mod_php
), php cannot detect that the client has aborted the connection until an attempt is made to send information to the client. If your long running script does not issue aflush()
the script may keep on running even though the user has closed the connection.Complicating things is even if you do issue periodic calls to
flush()
, having output buffering on will cause those calls to trap and won't send them down to the client until the script completes anyway!Further complicating things is if you have installed Apache handlers that buffer the response (for example
mod_gzip
) then once again php will not detect that the connection is closed and the script will keep on trucking.Phew.