Will PHP script be executed after header redirect?
Let me explain more. let's have an example using session.
$_SESSION["username"] = 'some username';
header("Location: another-file.php");
$_SESSION["username"] = 'replace username';
Result of $_SESSION["username"]
will be replace username
You can output a lot more headers than just Location
headers with header, most of which you don't want to stop your code execution. If you want to stop code execution, you need to call exit
explicitly.
The header
command doesn't interrupt the flow of your code. Even if that is encountered, your page is still downloaded by the browser, even if it isn't show. Consider 404 pages
, which (despite being errors) are still processed by the browser (though they are rendered while redirects are not).
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.
Yes, the script continues to process after the call to header('Location: http://google.com')
if you don't explicitly terminate it! I just tried this locally. I added test.php to a site in apache with these contents:
<?php
header('Location: http://google.com');
error_log("WE MADE IT HERE SOMEHOW");
?>
And checked my /var/log/apache2/error_log for this entry:
[Tue Feb 12 23:39:23 2013] [error] [client 127.0.0.1] WE MADE IT HERE SOMEHOW
Possibly surprising, but yes, it continues to execute if you don't halt execution.
People seem to get this really confused, and as to why the script is executed. When you send the redirect header the browser immediately redirects the user to the new page but your server will continue to process the code even though there is no one to receive it at the end, this doesn't mean the script is terminated. Unless you configure Apache to stop the script when it realizes there is no one on the other end.
When a PHP script is running normally the NORMAL state, is active. If the remote client disconnects the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button.
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. This behavior can be set via the
ignore_user_abort
php.ini directive as well as through the corresponding php_valueignore_user_abort
Apache httpd.conf directive or with theignore_user_abort()
function.
The correct way of redirecting a user without further actions is:
header("Location: blah.php");
exit();