91

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

0

6 Answers 6

97

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
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".
So exit is basically only for the server, so it won't do any more unnecessary work?
40

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.

3 Comments

I think you meant header IN exit()? ;)
Doesn't this mean you're passing the output of header as the exit code of exit? Wouldn't that be somewhat undesirable?
@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).
23

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.

Comments

9

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

2 Comments

Hello aromawebdesign.com, I do have code in formerror.php. Is that a problem? Thank you
if you do have a any kind of code after you call header, you should call die(); or exit();
6

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 ...

9 Comments

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.
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.
I've no idea what you're saying :/
@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?
@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.
|
-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.

4 Comments

I don't see why exit is even required here. Wouldn't PHP just "finalize processing" all the same?
@YourCommonSense: No it wouldn't. Who knows what else is coded. Processing should stop: hence the exit. Save precious resources at the earliest opportunity.
Then you should add HTML output too :)
@YourCommonSense: there is no HTML output only json. which is included in the exit. I suggest to look at the documentation . No reason for a down-vote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.