q0987 q0987 - 1 year ago 86
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download