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

php header location not working
php header(refresh)
php redirect without header
php exit not working
php redirect w3schools
how to redirect from one page to another in php
header exit
php redirect to another page

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

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

php - Why should I call exit() after calling Location: header?, The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply 

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:"));

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.

Why should I call exit() after calling Location: header? - php, There are many similar question like php - Should I call exit() after calling Location: header? and do i need to use exit after header("Location: http://​localhost/.

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.

header - Manual, Remember that header() must be called before any actual output is sent, either by header("Location:"); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */ exit; ?> You can do this by calling ob_start() and ob_end_flush() in your script, or setting the 

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

Web Database Applications with PHP and MySQL, In one - component querying , the response includes the Location header that The URL of the calling page is automatically initialized into the PHP web server The exit statement causes the script to abort after sending the header . A call to the header ( ) function should also be followed by an exit statement if no further 

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

Manual, exit is a language construct and it can be called without parentheses if no status define an exception named `SystemExit', throw it instead of calling exit() and catch it After sending the `Location:' header PHP _will_ continue parsing, and all 

PHP & MySQL: The Missing Manual, Just as she will often want to confirm a deletion before the deletion goes through, she So in reality, you can't send anything before calling header, and once you'​ve called header, you're not supposed to send anything after that. do happen, and that's why every call to a Location header is followed by that little exit() 

PHP for the Web: Visual QuickStart Guide, Tip PHP automatically runs ob_end_flush() at the conclusion of a script if it isn't with PHP, you send a location header: header('Location: page.php'); When using the header() function to redirect the browser, follow that line by calling exit() to to understand about using header() is that the function must be called before 

wp_redirect() | Function, Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit; : File: wp-includes/pluggable.php @param string $​location The path or URL to redirect to. Only use wp_redirect() when you are specifically trying to redirect to another site, and then you can 

  • possible duplicate of Why I have to call 'exit' after redirection through header('Location..') in PHP?
  • 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?
  • 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).
  • Hello, 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();
  • 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?
  • @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.