views:

899

answers:

6

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?

+3  A: 

It depends on your settings - usually it will stop but you can use ignore_user_abort() to make it carry on.

Greg
There's a big fat warning at http://www.php.net/manual/en/function.ignore-user-abort.php that `ignore_user_abort()` is not guaranteed to work if data cannot be sent to the client, which can happen in a number of scenarios. See my answer.
Crescent Fresh
+1  A: 

Depending on the configuration of the web server and/or PHP, the PHP process may, or may not, kill the thread when the user terminates the HTTP connection. If an AJAX request is pending when the user walks away from the page, it is dependent on the browser killing the request (not guaranteed) ontop of your server config (not guaranteed). Not the answer you want to hear!

I would recommend creating a work queue in a flat file or database that a constantly-running PHP daemon can poll for jobs. It doesn't suffer from cron delay but keeps CPU/memory usage to a usable level. Once the job is complete, place the results in the flat file/database for AJAX fetch. Or promise to e-mail the user once the job is finished (my preferred method).

Hope that helps

Al
+17  A: 

It depends.

From http://us3.php.net/manual/en/features.connection-handling.php:

When a PHP script is running normally the NORMAL state, is active. If the remote client disconnects the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button.

You can decide whether or not you want a client disconnect to cause your script to be aborted. Sometimes it is handy to always have your scripts run to completion even if there is no remote browser receiving the output. The default behaviour is however for your script to be aborted when the remote client disconnects. This behaviour can be set via the ignore_user_abort php.ini directive as well as through the corresponding php_value ignore_user_abort Apache httpd.conf directive or with the ignore_user_abort() function.

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 a flush() 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.

Crescent Fresh
very detailed +1
Alec Smart
A: 

If the client/user/downloader/viewer aborts or disconnects, the script will keep running until something tries do flush new data do the client. Unless you have used ignore_user_abort(), the script will die there. In the same order, PHP is unable to determine if client is still there without trying to flush any data to the httpd.

Havenard
A: 

There's some good, user contributed documentation on the php website.

Nathacof
A: 

found the actual solution for my case of it not terminating the connection. The SESSION on my Apache/Php server needed to close before the next one could start.

Browser waits for ajax call to complete after abort.

Artistan