为什么我必须调用“退出”?在 PHP 中通过 header('Location..') 重定向后?

发布于 2024-08-31 14:27:03 字数 309 浏览 5 评论 0原文

您知道,如果您想在 PHP 中重定向用户,您可以使用 header 函数:

header('Location: http://smowhere.com');

众所周知,在 header< 之后放置 exit; 也是一个很好的做法。 /code> 调用,以防止执行其他 php 代码。所以我的问题是: header-location 调用之后的代码能否有效执行?在哪些情况下?恶意用户能否完全忽略 header('Location..') 调用?如何?

You know that if you want to redirect a user in PHP you can use the header function:

header('Location: http://smowhere.com');

It is also well known that it is a good practice to put also an exit; after the header call, to prevent execution of other php code. So my question is: could the code after the header-location call be effectively executed? In which cases? Can a malicious user be able to completely ignore the header('Location..') call? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别念他 2024-09-07 14:27:03

标头位置调用之后的代码能否有效执行?

是的,总是如此。 header 只是一行要求浏览器重定向的数据。页面的其余部分仍将由 PHP 提供,并且客户端可以通过简单地阻止执行 header 命令来查看。

对于像 wget 这样的命令行客户端来说,这很容易做到,例如,只需告诉它不要遵循重定向即可。

底线:如果您不阻止它,即使在 header 调用之后,PHP 也会发送整个正文。该主体完全可供接收者使用,无需任何特殊的黑客技能。

could the code after the header-location call be effectively executed?

Yes, always. The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply preventing the header command from executing.

That is easy enough to do with a command-line client like wget, for example, by simply telling it not to follow redirects.

Bottom line: If you don't prevent it, PHP will send out the whole body even after a header call. That body is fully available to the recipient without any special hacking skills.

耳钉梦 2024-09-07 14:27:03

如果您重定向但没有 die() / exit() 代码始终会执行并显示

采取以下示例:

admin.php:

if (authenticationFails)
{
    // redirect and don't die
}

// show admin stuff

如果您不确保在位置标头之后结束执行,每个用户都将获得访问权限。

If you redirect but you don't die() / exit() the code is always executed and displayed.

Take the following example:

admin.php:

if (authenticationFails)
{
    // redirect and don't die
}

// show admin stuff

If you don't make sure to end the execution after the location header every user will gain access.

顾冷 2024-09-07 14:27:03

header() 指示 PHP 应发送 HTTP 标头...发送 HTTP 标头时。

当您编写对 header() 的调用时,这些不会立即发送,而是当需要发送它们时(通常,当 PHP 需要开始发送响应正文时——这可能比您想象的要晚,当启用output_buffering时)。

因此,如果您只调用 header(),则绝对不能保证不会执行此语句之后编写的代码 - 除非您通过使用 exit/<代码>死亡。

如果用户愿意,可以忽略 Location 标头;但它不会改变任何事实,即调用 header() 之后的代码可能会也可能不会被执行:那是服务器端的问题。

header() instructs PHP that a HTTP header should be sent... When the HTTP headers are sent.

And those are not sent immediatly when you write the call to header(), but when it's time to send them (typically, when PHP needs to begin sending the body of the response -- which might be later than you think, when output_buffering is enabed).

So, if you just call header(), there is absolutly ne guarantee that the code written after this statement is not executed -- unless you indicate that it must not, by using exit/die.

The user can ignore the Location header if he wants ; but it will not change anything on the fact that the code after the call of header() might or might not be executed : that matter is server-side.

ゞ花落谁相伴 2024-09-07 14:27:03

header() 之后的 PHP 代码将被运行。有时,这是必需的,如 php.net 上的 示例 所示。为了确保不是这样,您需要完全结束程序流程。

PHP Code after a header() will be run. Sometimes, that is required though, as the example on php.net shows. To make sure it's not, you end the program flow entirely.

ˉ厌 2024-09-07 14:27:03

如果没有退出调用,脚本终止的确切点/时间将取决于两个因素:

  1. 客户端浏览器对重定向做出反应的速度
  2. 执行脚本其余部分所需的时间。

假设浏览器在看到 Location 标头通过时立即启动重定向操作。这意味着它将关闭重定向来源的连接,以便它可以开始连接到新位置。这通常意味着 Web 服务器将终止重定向脚本。无论标头从服务器 -> 客户端发送以及 TCP 链接关闭过程从客户端 -> 服务器发送所需的时间,都是脚本可以继续运行的时间量。

Without the exit call, the exact point/time at which your script will terminate will come down to two factors:

  1. How quickly the client browser reacts to the redirect
  2. How much time it takes the rest of your script to execute.

Let's say the browser IMMEDIATELY starts the redirect action the moment it sees the Location header come through. That means it will shut down the connection from which the redirect comes, so it can start connecting to the new location. This generally means the web server will terminate the redirecting script. However long it takes for the header to go from server->client and the TCP link shutdown process to go from client->server is the amount of time in which your script can keep running.

森罗 2024-09-07 14:27:03

re: header-location 调用之后的代码可以有效执行吗?

可以,如果你不关闭脚本。

回复:在哪些情况下?

在每种情况下。

恶意用户能否完全忽略 header('Location..') 调用?

不,用户在此事上没有发言权。

re: could the code after the header-location call be effectively executed?

Yes if you don't close the script.

re: In which cases?

In every case.

Can a malicious user be able to completely ignore the header('Location..') call?

No, it will get exacted the user has no say in the matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文