84

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

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
q0987
  • 33,230
  • 67
  • 231
  • 369
  • 1
    possible duplicate of [Why I have to call 'exit' after redirection through header('Location..') in PHP?](http://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php) – rink.attendant.6 Oct 14 '14 at 17:52

5 Answers5

91

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

rgroli
  • 1,259
  • 1
  • 10
  • 19
  • Hello Oliver, Does the exit terminate the execution of script in formerror.php? I think my question is when I call 'exit' after 'header'. which script is affected and will not be executed anymore? Thank you – q0987 Aug 24 '10 at 13:28
  • 2
    exit always interrupts the current script (in your case "fileA"). The page you are redirecting to ("/formerror.php") is not affected at all. A redirection tells your browser to initiate a new HTTP-Request to the location you specified in "Location". It's basically the same like manually clicking a link to "formerror.php". – rgroli Aug 24 '10 at 14:06
  • So exit is basically only for the server, so it won't do any more unnecessary work? – Gust van de Wal May 22 '15 at 08:35
36

You should, just like @rgroli explains. If you do not want to bother with brackets, you can also call header() IN exit():

if(sth) exit(header("Location: http://example.com"));

Location header in HTTP/1.1 always requires absolute path see the note here.

Note: This is not a hack, since the exit code is used only if the parameter is integer, while header() produces void (it exits with code=0, normal exit). Look at it as exit_header() function like it should be after Location header.

Jan Turoň
  • 29,041
  • 21
  • 114
  • 159
  • 1
    I think you meant *`header` **IN** `exit()`*? ;) – yckart Jul 10 '16 at 19:39
  • Doesn't this mean you're passing the output of `header` as the exit code of `exit`? Wouldn't that be somewhat undesirable? – ADTC Nov 24 '16 at 09:45
  • @ADTC `header()` in PHP returns void and exit code is used only if the parameter is integer. In this case it just stops the code generation silently (implying exit code=0, normal exit). – Jan Turoň Nov 24 '16 at 10:02
21

It's generally good practice to exit; (note - it's a keyword, so you don't need the ()) after sending a Location: header, since browsers are supposed to redirect to the new page and so further execution of the current script is usually undesired.

Amber
  • 477,764
  • 81
  • 611
  • 541
8

If you don't have any code (PHP or HTML) under header, you don't have to.

Hydrino
  • 537
  • 3
  • 9
5

exit is bad coding.

if you ever develop a big project and want to create PHP Unit Testcases, exit will screw you up.

exit terminates the script and your running test! there is no way to recover the test and tell if it has failed or not ...

organise your code the way, that there is no output and the script ends naturally if you use a redirect ...

Benjamin Eckstein
  • 864
  • 2
  • 8
  • 19
  • 5
    I think that's a failure of the testing software if it cannot sandbox the script it is testing. It's like a computer shutting down because a shell script terminated using the `exit` command. – ADTC Nov 27 '16 at 07:53
  • 1
    no it is just wrong to terminate anything between anything. there is a stack for a reason behind programming. it all started with a function call and it all shall end with a function call. – Benjamin Eckstein Nov 30 '16 at 13:15
  • 5
    I've no idea what you're saying `:/` – ADTC Dec 01 '16 at 13:17
  • 1
    @Umingo Isn't it the very purpose of the `exit` call (or also `break`, inside a function/method)? Not only in PHP but almost every well-known programming language? – David Tabernero M. Aug 16 '18 at 17:08
  • @Davdriver no. exit will just prevent automatically testing your code and is bad style. its just a symptome of bad designed code to use exit. – Benjamin Eckstein Aug 17 '18 at 12:53
  • 1
    @Umingo Could you please post any source or example? You are just replying the same thing again, and it looks like an opinion more than a programming fact. – David Tabernero M. Aug 17 '18 at 14:45
  • 1
    @Davdriver write a phpunit test and try to check for failure if your script ends with an exit statement. it will give you a lot of headache. this post if 4 years old. let it rip ... – Benjamin Eckstein Aug 27 '18 at 11:21
  • Some editors auto append newline at the end of the file and then white character output can block the Location header. It can be solved by omitting the enclosing `?>` php tag, but it is confusing and not universal solution. `return 1` after the Location header could work with unit testing. – Jan Turoň Feb 06 '19 at 10:54
  • I know what Benjamin means- he just not want to provide example - It is just a lot more programming if not using exit- A good example is using IF - So instead of ` if ($condition == $bad) { exit(); } ` you should rather use ` if ($condition != $bad) { do_this(); } else { do_that_or_nothing(); } ` – Albuquerque Web Design Nov 30 '20 at 20:50