PHP:利用 exit(); 或者死(); 在 header("位置:");
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8665985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Utilizing exit(); or die(); after header("Location: ");
提问by Aaron
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?
我最近阅读了一篇关于exit();
并且die();
不知道我应该使用这些的帖子。据我了解,他们让它结束了 PHP?那是对的吗?我可以为此努力的最佳方法是什么,只需在 header() 之后直接添加这些函数之一;我有执行力?
I have AJAX, jQuery reading through my login.php/register.php, will this be affect in any way?
我有 AJAX、jQuery 读取我的 login.php/register.php,这会有任何影响吗?
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?
编辑:除了在 header(); 之后,我还应该在哪里使用exit();
ordie();
函数?并且exit();
更多地用于 PHP 而die();
更多地用于 Perl?
回答by GrayMatter
I have been looking for an answer on this as well. What I found:
我也一直在寻找这个问题的答案。我发现了什么:
Why die() or exit():
为什么 die() 或 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.
如果您没有header('Location: http://something')
在脚本之后放置 die() 或 exit() ,则可能会继续导致意外行为。例如,这可能会导致您实际上希望通过重定向 (HTTP 301) 阻止的内容被披露。上述内容对于最终用户可能不直接可见,因为浏览器可能不会呈现它(由于 301)。结论是,exit() 和 die() 函数会阻止脚本继续运行。
Difference:
区别:
I also wanted to know the difference between the functions as it seemsthere 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.
我也想知道这些功能之间的区别,因为它似乎没有。但是,在 PHP 中,Header 输出存在明显差异。在下面的示例中,我选择使用不同的标题,但为了显示 exit() 和 die() 之间无关紧要的区别。
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
Die() 在行动
<?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).
因此,die()关闭连接而exit()不会。是否要保持连接打开或关闭它取决于性能。两者都有优点和缺点,取决于您的具体要求。
回答by Timur
http://php.net/manual/en/function.exit.php
http://php.net/manual/en/function.exit.php
http://php.net/manual/en/function.die.php
http://php.net/manual/en/function.die.php
This functions are used to interrupt script execution. You needto 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.
该函数用于中断脚本执行。您需要在 之后使用exit
或die
停止执行您的脚本header("Location: " . getenv("HTTP_REFERER"));
,因为在其他情况下,您的脚本将一直执行到最后,这可能会导致一些意外行为。
回答by symcbean
Answer has already been accepted however it seems everyone is missing the glaring WTF in the question:
答案已被接受,但似乎每个人都错过了问题中明显的 WTF:
header("Location: " . getenv("HTTP_REFERER"));
Returning a referer is optional on the part of the user agent
it can easily be faked
there is no method for telling the user the login has failed
there is no HTTP semantic communication of an authentication failure
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
对于用户代理而言,返回引用者是可选的
它很容易被伪造
没有方法告诉用户登录失败
没有身份验证失败的 HTTP 语义通信
虽然环境变量 HTTP_REFERER 应该与请求头变量相同,但它没有在 RFC 3875 中指定,因此即使在请求中呈现给网络服务器, getenv("HTTP_REFERER") 也可能返回不同的值
回答by Wulfgier
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:
好的,距离上次给出答案已经很长时间了。无论如何 :D 不知何故,我偶然发现了一个类似的问题,看看我的解决方案是什么:
die( Header( "Location: mytarget.php?arg1=foobar" ) );
Two birds with one stone - seems to work for me.
一石二鸟-似乎对我有用。
回答by cPage
When header()
is called at the end of a script, there's no need to call exit()
, nor die()
since:
在header()
脚本末尾调用when ,无需调用exit()
,也无需调用die()
:
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
一旦脚本执行结束,到服务器的链接将立即关闭,除非它通过显式调用 mysql_close() 提前关闭。- php.net/function.mysql-connect
回答by jalodara jayesh
for($i = 0; $i < 10; $i++)
{
if ($i == 2)
{
exit("\n Using exit(), We are done");
}
}
Now let us look at the same example using die();
现在让我们看一下使用 die() 的相同示例;
for($i = 0; $i < 10; $i++)
{
if ($i == 2)
{
die("\n Using die(), We are done");
}
}
The output for each of these will be: “Using exit(), We are done Using die(), We are done” respectively. Now let us try each of these to output a number.
每一个的输出将分别是:“使用 exit(),我们完成了使用 die(),我们完成了”。现在让我们尝试每一个输出一个数字。
for($i = 0; $i < 10; $i++)
{
if ($i == 2)
{
exit(-1);
}
}
Looking at the output for the same example using die();
使用 die() 查看同一示例的输出;
for($i = 0; $i < 10; $i++)
{
if ($i == 2)
{
die(-1);
}
}
These examples were edited using a textpad editor. The output for both the above cases were “Tool completed with exit code -1”.
这些示例是使用 textpad 编辑器编辑的。上述两种情况的输出都是“工具完成,退出代码 -1”。
So, the honest answer to the question “What is difference between die() and exit() in php” IS – There is not a single visible difference between these two functions. They both are the same, one is the alias of the other. If at all any one of you can find a real visible difference then I would appreciate if you could post it in the comments section of this blog.
因此,对“php 中 die() 和 exit() 之间有什么区别”这个问题的诚实回答是——这两个函数之间没有明显的区别。它们都是一样的,一个是另一个的别名。如果你们中的任何一个人都能找到真正明显的差异,那么如果您能将其发布在本博客的评论部分,我将不胜感激。
Till next time, Happy PHP programming!
直到下一次,PHP 编程快乐!