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

微笑、不失礼 提交于 2019-11-26 14:41:30

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

http://php.net/manual/en/function.exit.php

http://php.net/manual/en/function.die.php

This functions are used to interrupt script execution. You need to use exit or die to stop execution of your script after header("Location: " . getenv("HTTP_REFERER"));, because, in other case, your script will be executed till the end, what can cause some unexpected behavior.

Answer has already been accepted however it seems everyone is missing the glaring WTF in the question:

header("Location: " . getenv("HTTP_REFERER"));
  1. Returning a referer is optional on the part of the user agent

  2. it can easily be faked

  3. there is no method for telling the user the login has failed

  4. there is no HTTP semantic communication of an authentication failure

  5. while the environment variable HTTP_REFERER should be the same as the request header variable, it is not specified in RFC 3875, therefore even where presented to the webserver in the request, getenv("HTTP_REFERER") may return a different value

cPage

When header() is called at the end of a script, there's no need to call exit(), nor die() since:

The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close(). - php.net/function.mysql-connect

Ok, it has been a long time since the last answer was given. Anyway :D somehow I stumbled across a similar prob and see what my solution was:

die( Header( "Location: mytarget.php?arg1=foobar" ) );

Two birds with one stone - seems to work for me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!