开发者

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

For example, there is a开发者_JAVA百科 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.


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.


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.


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜