Oguz Bilgic
Oguz Bilgic

Reputation: 3480

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

For example, there is a very simple PHP script which updates some tables on database, but this process takes a long time (maybe 10 minutes). Therefore, I want this script to continue processing even if the user closed the browser, because sometimes users do not wait and they close the browser or go to another webpage.

Upvotes: 26

Views: 24464

Answers (8)

hostingutilities.com
hostingutilities.com

Reputation: 9509

From the documentation:

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.

So by default, PHP will not continue running after the user closes the browser.

To change this on a one-off basis, use:

ignore_user_abort(true);

To change the default behavior for the entire application, add to your php.ini file:

ignore_user_abort = On

The documentation page also mentions that a function registered with the register_shutdown_function() function will always be run, and a timeout can also cause a script to end early. See the documentation for more info on those scenarios.

For long running tasks

In addition to timeouts, remember that there are other things that could interrupt your script such as hitting resource limitations or the server or Apache/Nginx getting restarted. For tasks that take hours to run, having to restart such a long-running task could be quite annoying. If you have such a long running task, consider looking for ways of breaking the task down into smaller tasks that can be queued up and then executed by code that is started from a cron job, or by a service listening to events from a message broker, or by a background daemon. See best ways to manage long running PHP scripts.

Upvotes: 0

bcoughlan
bcoughlan

Reputation: 26617

The PHP script will keep running after the client terminates the connection (not doing so would be a security risk), but only up to max_execution_time (set in php.ini or through a PHP script, generally 30 seconds by default)..

For example:

<?php
    $fh = fopen("bluh.txt", 'w');
    for($i=0; $i<20; $i++) {
        echo $i."<br/>";
        fwrite($fh,$i."\n");
        sleep(1);
    }
    fclose($fh);
?>

Start running that in your browser and close the browser before it completes. You'll find that after 20 seconds the file contains all of the values of $i.

Change the upper bound of the for loop to 100 instead of 20, and you'll find it only runs from 0 to 29. Because of PHP's max_execution_time the script times out and dies.

Upvotes: 6

DavidWinterbottom
DavidWinterbottom

Reputation: 6540

If the task takes 10 minutes, do not use a browser to execute it directly. You have lots of other options:

  • Use a cronjob to execute the task periodically.
  • Have the browser request insert a new row into a database table so that a regular cronjob can process the new row and execute the PHP script with the appropriate arguments
  • Have the browser request write a message to queue system, which has a subscriber listening for such events (which then executes the script).

While some of these suggestions are probably overkill for your situation, the key, combining feature is to de-couple the browser request from the execution of the job, so that it can be completed asynchronously.

If you need the browser window updated with progress, you will need to use a periodically-executed AJAX request to retrieve the job status.

Upvotes: 22

anonymous
anonymous

Reputation: 163

Yes. The server doesn't know if the user closed the browser. At least it doesn't notice that immediately.

No: the server probably (depending of how it is configured) won't allow for a php script to run for 10 minutes. On a cheap shared hosting I wouldn't rely on a script running for longer than a reasonable response time.

Upvotes: 0

timdev
timdev

Reputation: 62874

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.

Upvotes: 19

Annerajb
Annerajb

Reputation: 932

You can make the PHP script run every 20 minutes using a crontab file which contains the time and what command to run in this case it would be the php script.

Upvotes: 1

yopefonic
yopefonic

Reputation: 532

if the script is completely server based (no feedback to the user) this will be done even if the client is closed.

The general architecture of PHP is that a clients send a request to a script that gives a reply to the user. if nothing is given back to the user the script will still execute even if the user is not on the other side anymore. More simpler: their is no constant connection between server and client on a regular script.

Upvotes: 2

shanyu
shanyu

Reputation: 9716

A server-side script will go on what it is doing regardless of what the client is doing.

EDIT: By the way, are you sure that you want to have pages that take 10 minutes to open? I suggest you to employ a task queue (whose items are executed by cron on a timely basis) and redirect user to a "ok, I am on it" page.

Upvotes: -1

Related Questions