The code should look like this: -
$flag=1; if($flag==1) { header("Location:page1.php"); exit(); } header("Location:page2.php"); exit();
If you do not use the " exit() " / " die() " construct, PHP will continue to execute the following lines. This is due to the fact that PHP redirects the user to the first page (in this case, " page1.php "), but internally after executing all the statements written on the entire page, even after executing the " header() " method. To stop this, we need to use either the " exit() " / " die() " constructs.
Hope this helps.
source share