Reputation: 4166
Here I have written that if...else condition with different header location.
$a = "yahoo";
if($a == "yahoo")
{
header('location:http://www.yahoo.com');
}else{
header('location:http://www.gmail.com');
}
header('location:http://www.google.com');
Problem:
In above code if condition is true then also it will redirecting to google.com
As per my opinion I think its first goes into if condition and then redirect to given location and other below code will not executed.
When I will write exit()
or die()
with every header It will work.
Question
Can anyone suggest me where its create issue?
Why without exit()
or die()
its not working?
Upvotes: 0
Views: 246
Reputation: 579
as you can find in this comment on docs: http://php.net/manual/en/function.header.php#85254
Headers are overwriten by themselfs. Therefore if you do not stop executing your code, your header is changed to 'google' and after that sent to the user..
Upvotes: 1
Reputation: 581
The "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
You can read more about this at the php manual php header
Upvotes: 2