Why the execution goto after redirection using header()
$flag=1;
if($flag==1)
header("Location:page1.php");
header("Location:page2.php");
when use this code page redirects to page2.php, Why its happen
You need to put an exit;
after the header call; PHP does not automatically stop executing code after the client stops loading the page.
The code should be like:-
$flag=1;
if($flag==1) {
header("Location:page1.php");
exit();
}
header("Location:page2.php");
exit();
If you don't use the "exit()
" / "die()
" construct, PHP will continue to execute the next lines. This is because PHP redirects the user to the first-mentioned page (in this case it's "page1.php
"), but internally after executing all the statements written in the whole page, even after the "header()
" method is executed. To stop this, we need to use either the "exit()
" / "die()
" constructs.
Hope it helps.
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