I was writing a script where I have a redirect header in the end.
Then I added a redirect header in the middle if a specific condition would come up.
I was very surprised to notice that the last header(“Location:http//mydomain.com”) was the one that triggered. I thought that the header would automatically “jump” from the page, but it seems like it just keeps it in memory and goes through the whole page first.
Any ideas on what the “proper” way to “cut” the flow and change page is?
<?php
$specific_condition=true;
// I wanted this to be the end
if($specific_condition) header("Location: http://google.com/");
// But it still triggers following redirect
header("Location: http://bing.com/");
?>
Always call
exit;right after yourLocationheader:header()function by itself just collects the headers to be sent to the client. So it is not supposed to stop execution after any particular header, e.g.Location. But as long as it makes no sense to do something after you decided to redirect user –exitis used