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

后端 未结 7 1540
日久生厌
日久生厌 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:

    ";
            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.

提交回复
热议问题