do I need to use exit after header("Location: http://localhost/...");?

php exit
php header
php redirect
php exit 500
php exit vs return
php break or exit
php stop script after time
php header 302

I'm creating a script to validate a form and I was asking myself a question. When I use a header (see example below), do I need to use exit right after? I mean, does using header also means that it is exiting by default therefore I don't need to use the command exit?

// cancel button clicked
if (isset($_POST['cancel'])) {
  header("Location: http://localhost/admin/tracks.php");
  exit;
}

echo '<p>$name</p>'

You should call exit() because a header() won't automatically stop the script from executing - or if it does (I'm honestly not 100% on that), it definitely doesn't stop the script instantly.

For example, try this code:

<?php

  header("Location: http://www.google.com");
  unlink(__FILE__);

?>

This little script uses header() to redirect you to Google, and then deletes itself. If you run it, you'll notice that after you were redirected, the file was still deleted. This means that the unlink() call was still executed even though the header() call redirected you.

exit(); after header("Location: shirts.php"); is it really necessary?, exit() terminates the current script. My basic understanding of it is that the header function doesn't immediately terminate the connection and the code after can still be executed. So adding exit after header is to make sure that code after it won't be executed. It does NOT stop your script from running, your script will keep on running and sometimes all a person (could be with bad intentions) needs is your script to reach a certain point where he could do X. Header() will just redirect, exit(); however will stop the script right on the spot (where exit(); is). or as someone else stated under the username:

I use exit after the header->location call because I want to be able to rely ABSOLUTELY on the fact that the script won't get past the header->location call.

If there's a bug somewhere and your script starts generating output BEFORE the header->location call, the call will fail, and script execution will continue normally (unless you call exit)

Why we write "exit ; " after redirect command ? header("Location , just to make sure that the code after redirect line will not be executed. We can use this to prevent from the recipient to perform the special hacking skills. using header function we have Make sure that code below does not  exit() Prototype: void exit(int ExitCode); Header File: stdlib.h (C) or cstdlib (C++) Explanation: Exit ends the program. The ExitCode is returned to the operating system, similar to returning a value to int main.

Although the answers above sound great, if you're unsure of your code path, this could lead to unexpected results. For example, if you're using a framework that relies on the fact that code execution will run from beginning to end, you may inadvertently interrupt it.

This might be okay from a user perspective as they will still be redirected and will be none the wiser, but consider the following:

You're using a framework (OS or custom) that is expecting to log redirects, or set additional headers (or any number of other items). By calling exit, you're circumventing that logic and therefore may get unexpected results.

So in short, yes the above methods will work, just a word of caution to know what you're expecting to happen before short circuiting it.

Safely handling redirects with die() and exit() in PHP, php , tools that do not follow the Location header automatically will still be able to see the remainder of the HTTP response. This is easily  You can close the header and footer editing by clicking in the body of the document or by selecting the Close Header and Footer button on the Header and Footer Tool Design feature. For more information and resources, visit our Microsoft Word Skills Page. Need additional help?

Output is generally not sent (depending on output buffering and so on) if you redirect like that, but as shown by the unlink() example the script does not die with header().

So the exit() or die() calls are necessary if you want to prevent the script from continuing after the redirect.

php, There are many similar question like php Should I call exit after calling Location header and do i need to use exit after headerquotLoc Any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal. Declaration. Following is the declaration for exit() function. Parameters. status − This is the status value returned to the parent process.

It does NOT stop your script from running, your script will keep on running and sometimes all a person (could be with bad intentions) needs is your script to reach a certain point where he could do X. Header() will just redirect, exit(); however will stop the script right on the spot (where exit(); is). or as someone else stated under the username:

Cody. A. Ray: Yes, the script continues to process after the call to header('Location: http://google.com') if you don't explicitly terminate it! I just tried this locally. I added test.php to a site in apache with these contents

<?php

 header('Location: http://google.com');
 error_log("WE MADE IT HERE SOMEHOW");

?>

And checked my /var/log/apache2/error_log for this entry:

 [Tue Feb 12 23:39:23 2013] [error] [client 127.0.0.1] WE MADE IT HERE SOMEHOW

so end conclusion: Header doesn't stop the script from running.

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/. Meetups for people interested in making things with Raspberry Pi computers Thousands of clubs need volunteers like you Thanks to our community of thousands of volunteers around the world, over a million young people have already learned about digital making in a fun and accessible way.

How to make a redirect in PHP?, Note: The die() or exit() function after header is mandatory. If die() or exit() is not put How to use Meta Tag to redirect an HTML page? How to redirect Which language should you learn first if you want to make an app? How to make a div  How to Insert a Running Head for APA Style in Word If the issue is with your Computer or a Laptop you should try using Reimage Plus which can scan the repositories and replace corrupt and missing files.

PHP: header - Manual, Make sure that code below does not get executed when we redirect. */ exit; ?> Only use the Expires header, after all, it will require the file to be downloaded  The charts on this page illustrate just how little power you trade off for the much lower cost and easy installation of compact headers. We build hundreds of custom fabricated headers for one-of-a-kind cars, but for most vehicles you can get the performance you need with a set of Sanderson's right from our extensive product line.

Redirection After HTML OutPut - PHP, If you want any code below this header function to be ignored, simply add exit after it. If you simply want a timed redirect you can use a meta tag eg. After you have determined the header values, include them when you make public folder hierarchy requests. The steps in this article are specific to public folder hierarchy requests. To determine whether your request is a public folder hierarchy or content request, see Routing public folder requests .

Comments
  • I think you don't need to. When the header is sent, you will be redirected and the script will stop the execution.
  • @EmCo Not exactly true. See the example in my answer.
  • possible duplicate of Why I have to call 'exit' after redirection through header('Location..') in PHP?
  • I've had the same concern and it does continue executing! Best to exit;
  • Good point about the failed call. I hadn't even considered that possibility.
  • Your bug example is irrelevant. No bugs required for script to continue
  • @Shrapnel I didn't say one was required, I offered an example. Script execution does not continue normally after a successful header->location call
  • @Shad: Actually, it does. See my answer for an example. Your first "Location" header doesn't even need to be the first one you send. You can set a Location header, do some processing, then send another one, and the user will be redirected to the last Location you specify.
  • @Shad oh, really? any reason for that?