q0987 q0987 - 15 days ago 10
PHP Question

php - Should I call exit() after calling Location: header?

After calling the redirect function header, should I call exit or not?

<?php // fileA
$urlFailToGoTo = '/formerror.php';

if (sth)
{
header(sprintf("Location: %s", $urlFailToGoTo));
exit(); //should I call exit() here? or return?
}

?>


Thank you

Answer

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect.