Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Utilizing exit(); or die(); after header("Location: ");

Tags:

php

exit

die

I have a user login/registration system that simply uses

// execute queries, set cookies, etc. here
header("Location: " . getenv("HTTP_REFERER"));

I recently read a post about exit(); and die(); and had no idea that I was supposed to be using these. From what I understand, they make it end the PHP? Is that correct? What's the best way I can work toward this, simply adding one of these functions directly after ever header(); execution I have?

I have AJAX, jQuery reading through my login.php/register.php, will this be affect in any way?

Edit: Other than after header();, where else should I be usitilizing the exit(); or die(); functions? And is exit(); more used around PHP whereas die(); more used around Perl?

like image 925
Aaron Avatar asked Dec 29 '11 09:12

Aaron


People also ask

Should I use exit after header in PHP?

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect. exit always interrupts the current script (in your case "fileA").

What is PHP header location?

Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory. replace: It is optional which indicates whether the header should add a second header or replace previous.


1 Answers

I have been looking for an answer on this as well. What I found:

Why die() or exit():

If you don't put a die() or exit() after your header('Location: http://something') your script may continue resulting in unexpected behaviour. This may for example result in content being disclosed that you actually wanted to prevent with the redirect (HTTP 301). The aforementioned may not directly be visible for an end user as the browser may not render it (due to the 301). Conclusion, the exit() and die() functions stop the script from continuing.

Difference:

I also wanted to know the difference between the functions as it seems there is none. However, in PHP, there is a distinct difference in Header output. In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter.

Exit() in action

<?php
    header('HTTP/1.1 304 Not Modified');
    exit();
?>

Results in:

HTTP/1.1 304 Not Modified 
Connection: Keep-Alive 
Keep-Alive: timeout=5, max=100

Die() in action

<?php
    header('HTTP/1.1 304 Not Modified');
    die();
?>

Results in:

HTTP/1.1 304 Not Modified 
Connection: close

Difference

So, die() closes the connection and exit() doesn't. It depends on performance whether or not you want to keep the connection open or close it. Both have advantages and disadvantages and depends on your specific requirement(s).

HTTP persistent connections on Wiki

like image 73
GrayMatter Avatar answered Sep 18 '22 05:09

GrayMatter