Why the Location header does not redirect the page immediately? It always performs the entire process before redirecting?
I will give some example:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With