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

后端 未结 7 1507
日久生厌
日久生厌 2020-12-09 15:46

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

相关标签:
7条回答
  • 2020-12-09 16:13

    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.

    0 讨论(0)
  • 2020-12-09 16:18

    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.

    0 讨论(0)
  • 2020-12-09 16:25

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 16:31

    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.

    0 讨论(0)
  • 2020-12-09 16:34

    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 讨论(0)
提交回复
热议问题