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

前端 未结 5 906
耶瑟儿~
耶瑟儿~ 2020-11-30 05:02

if i call a php file via jquery ajax, that contains a script to do some stuff that takes a while — for instance uploading a big video — and then I close the page: does the p

相关标签:
5条回答
  • 2020-11-30 05:12

    The script will run the time set by max_execution_time (default is 30s)

    Warning This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini. Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real. quote from http://php.net/manual/en/function.set-time-limit.php

    you can test this by running

    <?php 
    unlink('cocorico.txt'); 
    while(true){ 
        file_put_contents('cocorico.txt', microtime(true).PHP_EOL, FILE_APPEND); 
    } 
    

    and it will stop after 30s (despite you close your browser or not) you can get you default exec time by echo ini_get('max_execution_time'); and can be set like set_time_limit(3); The answer marked as accepted is only correct about the ignore_user_abort but don't panic that your "fail" scripts will run forever if you don't set max exec time to 0 - unlimited;

    0 讨论(0)
  • 2020-11-30 05:19

    By default no. See Connection Handling documentation, especially:

    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.

    0 讨论(0)
  • 2020-11-30 05:20

    A PHP Script running through a web server will not stop until:

    • someone kill the server
    • the server kill the php scrip

    When the user abort the script, PHP will continue until it try to send something back to the browser.

    For example still script will continue fore ever even if the user abort:

    while(true){
        echo 'go'.PHP_EOL;
    }
    

    It will go on forever because the "echo", will write into the buffer, and the buffer will not be sent to the browser until the script finish, which will never happen.

    The following script will stop as soon as the user abort:

    while(true){
        echo 'go'.PHP_EOL;
        flush();
        ob_flush();
    }
    

    This script will stop, because flush() and ob_flush() will force PHP to send its buffer to the browser, which will stop the PHP script if the user has aborted. The function ignore-user-abort() will force PHP to ignore the abort in this case.

    Moreover if you are using PHP session, they are another tricky situation. For example, if you are doing AJAX, and you actually send two AJAX request to a PHP script and that PHP script has need of session with session_start(). The first AJAX query will work normally, however the second one will have to wait until the first call is finish, because the first script has a locked on the session. The first script could eventually prematurely release the session with session_write_close();

    0 讨论(0)
  • 2020-11-30 05:22

    From my little understanding of how these stuff works. By the point of view of the HTTP protocol I would say yes, the script would keep running, because the browser just sends a request to the server asking for the page, then the server starts executing the script and does not sends or receives information from the browser untill the script is done loading and producing the html output, and just then the server sends the resulting output to the browser and has done the job.

    See, there is no way for a browser to "tell" the server that the user is not viewing the page anymore through the HTTP protocol. However, the HTTP protocol runs on top of the TCP connection through stream sockets, the TCP connection is kept alive till one of the ends choses to abort the connection (or a certain timeout is reached), now I really don't know how the browser handles this. The browser could just open a connection, send a request and close the connection, then the server waits for the script and sends the response on another connection. Or the browser could open a connection, KEEP this connection alive till the server responds on the same connection. If the thing works that way then the server would really have a way to know if the user is not viewing the page anymore simply by checking if the connection is still alive or has been shutdown by the client. So that would be a no.

    Dunno much about that tho.

    0 讨论(0)
  • 2020-11-30 05:26

    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.

    0 讨论(0)
提交回复
热议问题