2

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

I dont know how to title this question but here it goes.

what is the difference between the two code snippets below:

script1 - without exit():

if($var = true){
header('Location: anotherpage.php');
}

script2 - with exit():

if($var = true){
header('Location: anotherpage.php');
exit();
}

from what I understand, header() exits the current page and jumps to another page if the condition is true therefore having exit() after header() is pointless - am I right?

Community
  • 1
  • 1
anagnam
  • 457
  • 6
  • 23

2 Answers2

3

The header() function just sends a header to the browser along with the rest of your page which tells the browser to redirect. If you don't want the script to continue running then you should do an exit()

There may be cases where you want the script to continue running though as well, depending on the script. You may be keeping track of page hits or something and you might have code insert that into a database included at the bottom of every page. If you want it to track the hit before the redirect then you wouldn't want to exit early.

in most cases you do want to exit because you do not likely want to output anything to the browser in that case and the extra code will just slow down your redirect.

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

No. I am assuming that the if ($var = true) should have read if ($var == true) or even just if ($var)

The header function adds data to the HTTP header. If you have nothing after the if statement above then both are the same. But if you have code after the if statement that code will be executed - something that you do not wish to happen.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127