Does PHP Execution Stop After a User Leaves the Page

Does php execution stop after a user leaves the page?

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.

Will a script continue to run even after closing a page?

See here:
http://php.net/manual/en/function.ignore-user-abort.php

int ignore_user_abort ([ bool $value ] )

Sets whether a client disconnect should cause a script to be aborted.

When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless value is set to TRUE

There also is a PHP configuration option of the same name:
http://php.net/manual/en/misc.configuration.php

By default, if you do nothing, according to the PHP manual the default is to abort the script.
http://php.net/manual/en/features.connection-handling.php

NECESSARY UPDATE

It seems I (unknowingly) tricked my way to "reputation points", because I did NOT supply the (correct) answer, but here it is now thanks to testing and continued nudging from "mellamokb":

Quote:
"Ok, I took a look at the PHP source code and, if I didn't miss anything, I now have the answer. The "ignore_user_abort" flag is only checked when PHP receive an error trying to output something to the user. So, in my understanding, there is no way to interrupt code which doesn't produce any output."

Okay, I wasn't totally off, but it is important to know that it all depends on whether or not your script produced any output!

If you read THIS, also DO check out the comments below.

What happens if a user exits the browser or changes page before an AJAX request is over

As long as the user agent (browser, etc.) has fully sent the request, the server has all it needs and will complete the request and try to send back a response.

In fact, this sort of "pinging" behavior is often used for "heartbeat"-like processes that keep a service warm or perform periodic maintenance.

php code canot stop after user abort, it runs until the script is completed

Closing the browser doesn't tell the server to stop doing something. It doesn't tell the server anything.

Long-running processes don't belong in web applications. Generally you would want some background task to handle the process. Either the web application would spawn this task (this seems like a workable approach) or would in some way queue the processing of this task where a background worker would see that queue (such as a database table polled every X minutes by a daemon process).

The goal is to not block the UI while the task is running. Even if the user were to leave the browser open, the browser itself may "give up" after a while or something else could sever the user from the UX while waiting for too long. Let the user invoke the process, but separate the invocation of the process from the execution of the process so the user can return to the application interface.

PHP script continues after closing / stopping page

You could use ignore_user_abort but I think it only applies if you are running PHP as a command line script .

When running scripts from browser , in my experience I have realized scripts running even after the browser was closed . I did not go further to check how long they used to run .

The scripts that does huge processing that could run many minutes , I used to have control as below :

  • A flag variable is considered which is stored in a database table .
  • A way is provided to set the flag On or Off ( or 1 or 0 ) .
  • The flag is checked in the script and the script is stopped running if the flag is found to be Off ( or 0 ) .

One way to consider the flag is in a loop as below :

while(true)
{
/* Your code here which probably does lot of processing */

if($flag === false) break; // Or exit if you prefer
}

If you have code that need to run following the loop , you would use break .
If you want to abruptly stop the script you could use exit instead in its place .

Can PHP scripts continue to run even if the user closed the browser?

To answer your question directly, see ignore_user_abort

More broadly, you probably have an architecture problem here.

If many users can initiate this stuff, you'll want the web application to add jobs to some kind of queue, and have a set number of background processes that chew through all the work.

Can closing the browser terminate the PHP script on the server?

PHP won't notice that the browser closed the connection unless it tries to output something (e.g. echo). If it fails to output something, the script will be terminated unless ignore_user_abort is On.

So the script will terminate only if the following conditions are met:

  • the script attempts to output something (because the script won't notice that the user aborted the connection until then)
  • AND php's ignore_user_abort setting is off (if the setting is off, php will terminate the script if fails to output something)

You can avoid the script from terminating by enabling ignore_user_abort.

You can use connection_aborted() at anytime to check is the browser aborted the request.

A script can terminate unexpectedly for other reasons (e.g. max execution time, exception, etc); so you should make use of transactions, so that half-finished changes are canceled if the script terminates abnormally.

Do Ajax requests still finish when leaving the page?

Once a request is sent to the server, irrespective of whether you navigate away from the page the server side of the request will get completed.

The only thing that will not happen is the execution of client side callback method.



Related Topics



Leave a reply



Submit