php header Location immediately?
Why the Location header does not redirect the page immediately? It always performs the entire process before redirecting?
I will give some example:
开发者_C百科header('Location:http://www.php.net');
$f = fopen('demo.txt','w+');
fwrite($f,'Test');
fclose($f);
It always generates the TXT file before redirecting to http://www.php.net
.
Well, header()
just sends certain header to browser. PHP still continues running the script after that. If you don't want to stop running the script, just use die;
or exit;
- it will stop processing the script further.
It's because the header()
function does not redirect, it sends a header. A browser may (theoretically) completely ignore it and choose to continue parsing the page. If you wish the script to not process after sending the header, fire die()
or exit()
immediately after.
Why the Location header does not redirect the page immediately?
Just because it doesn't redirect anything at all. It's browser who will brake current connection (causing script to stop) to requests another page. And there is a network latency.
It always performs the entire process before redirecting?
Not always. It's just not guaranteed.
I really need the process to continue affixes the header location in order to finalize the system and generate logs
with mod_php you will need ignore_user_abort()
and with php-fpm it's fastcgi_finish_request()
to guarantee entire script execution.
精彩评论