The header()
call does not stop the execution of your script immediately. If you redirect but you don't call exit()
, then the code is always executed.
To demonstrate this issue, you can consider the following code:
header('Location: http://google.com');
file_put_contents('file.txt', 'I was executed, YAY!');
It will redirect you to Google, but will also output the text in file.txt
. It proves that header()
calls don't necessarily stop the script execution. You should always use exit()
to make sure that the script isn't executed further.