Php header

Why doesn't the Location header redirect the page immediately? Does it always complete the whole process before redirecting?

I will give an example:

header('Location:http://www.php.net'); $f = fopen('demo.txt','w+'); fwrite($f,'Test'); fclose($f); 

It always generates a TXT file before redirecting to http://www.php.net .

+4
source share
3 answers

Well, header() just sends a specific header to the browser. After that, PHP continues to run the script. If you do not want to stop the script from starting, just use die; or exit; - it will stop processing the script further.

+10
source

This is because the header() function does not redirect; it sends the header. The browser can (theoretically) completely ignore it and continue to parse the page. If you want the script to not be processed after the header is sent, immediately fry die() or exit() .

+2
source

Why doesn't the Location heading redirect the page right away?

Just because it does not redirect anything at all. This is a browser that will block the current connection (which will cause the script to stop) to request another page. And there is network latency.

Does it always complete the whole process before redirecting?

Not always. This is simply not guaranteed.

I really need a process to continue to attach the location of the header to shut down the system and generate logs

with mod_php you will need ignore_user_abort() and with php-fpm it fastcgi_finish_requestβ€Ž() to guarantee the execution of the whole script.

+1
source

Source: https://habr.com/ru/post/1369272/


All Articles