90

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
  • 156,878
  • 40
  • 214
  • 345
q0987
  • 34,938
  • 69
  • 242
  • 387

6 Answers6

94

You definitely should. Setting header alone does not terminate the script execution.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
rgroli
  • 1,339
  • 1
  • 11
  • 20
  • 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
38

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ň
  • 31,451
  • 23
  • 125
  • 169
  • 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
  • 507,862
  • 82
  • 626
  • 550
8

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

Hydrino
  • 567
  • 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
  • 884
  • 2
  • 9
  • 19
  • 6
    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
-1

REST

A related but not identical case is when implementing a REST api. In this case the body is supposed to contain XML or JSON (or some other esoteric form) so after a payload is established as an array or object and all appropriate headers were provided this would finalise processing:

header( 'Content-Type: application/json' );
exit(json_encode($payload));

or

header( 'Content-Type: application/xml' );
exit(xmlrpc_encode($payload));

Both would return the payload as body content and stop processing immediately freeing up server resources.

theking2
  • 2,174
  • 1
  • 27
  • 36
  • 1
    I don't see why exit is even required here. Wouldn't PHP just "finalize processing" all the same? – Your Common Sense Jul 09 '23 at 17:51
  • @YourCommonSense: No it wouldn't. Who knows what else is coded. Processing should stop: hence the exit. Save precious resources at the earliest opportunity. – theking2 Jul 09 '23 at 18:41
  • 1
    Then you should add HTML output too :) – Your Common Sense Jul 09 '23 at 18:49
  • @YourCommonSense: there is no HTML output only json. which is included in the exit. I suggest to look at the [documentation](https://www.php.net/manual/en/function.exit.php) . No reason for a down-vote. – theking2 Jul 09 '23 at 19:02