Will PHP script be executed after header redirect?

前端 未结 10 2054
天涯浪人
天涯浪人 2020-12-03 13:41

Yes, this question has been asked before, however, the answers have been inconsistent. Take Why I have to call 'exit' after redirection through header('Location

10条回答
  •  無奈伤痛
    2020-12-03 13:52

    Running the code:

    //http://www.php.net/manual/en/function.header.php
    header('Location: http://google.com');
    flush();
    sleep(3);
    
    $a=fopen('test.txt', 'w');
    fwrite($a,headers_sent());
    fclose($a);
    

    The server paused and wrote the file before the client redirected me. This is because, even after flush()ing the buffer, the the redirect is not processed until the script ceases execution (ie, the script terminated). The file test.txt had '1' in every case, meaning that the headers were sent, just not processed by the browser until the connection was terminated.

    • in every case, meaning on a win32 development machine, linux development machine, and a linux production environment.

提交回复
热议问题