Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is code after header("Location: ...) executed?

$_SESSION["some_value"] = 4; header("Location: another-file.php"); $_SESSION["some_value"] = 5; 

what's the value of $_SESSION["some_value"] ?

like image 269
Mawg says reinstate Monica Avatar asked Sep 12 '10 02:09

Mawg says reinstate Monica


People also ask

Why exit after header?

exit() terminates the current script. My basic understanding of it is that the header function doesn't immediately terminate the connection and the code after can still be executed. So adding exit after header is to make sure that code after it won't be executed.

Should I use exit after header in PHP?

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect. exit always interrupts the current script (in your case "fileA").


1 Answers

The value is 5.

You can output a lot more headers than just Location headers with header, most of which you don't want to stop your code execution. If you want to stop code execution, you need to call exit explicitly.

like image 103
deceze Avatar answered Sep 24 '22 13:09

deceze