为什么我必须调用“退出”?在 PHP 中通过 header('Location..') 重定向后?
您知道,如果您想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,总是如此。
header
只是一行要求浏览器重定向的数据。页面的其余部分仍将由 PHP 提供,并且客户端可以通过简单地阻止执行header
命令来查看。对于像 wget 这样的命令行客户端来说,这很容易做到,例如,只需告诉它不要遵循重定向即可。
底线:如果您不阻止它,即使在
header
调用之后,PHP 也会发送整个正文。该主体完全可供接收者使用,无需任何特殊的黑客技能。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 theheader
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.如果您重定向但没有
die()
/exit()
代码始终会执行并显示。采取以下示例:
admin.php:
如果您不确保在位置标头之后结束执行,每个用户都将获得访问权限。
If you redirect but you don't
die()
/exit()
the code is always executed and displayed.Take the following example:
admin.php:
If you don't make sure to end the execution after the location header every user will gain access.
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 usingexit
/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 ofheader()
might or might not be executed : that matter is server-side.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.
如果没有退出调用,脚本终止的确切点/时间将取决于两个因素:
假设浏览器在看到 Location 标头通过时立即启动重定向操作。这意味着它将关闭重定向来源的连接,以便它可以开始连接到新位置。这通常意味着 Web 服务器将终止重定向脚本。无论标头从服务器 -> 客户端发送以及 TCP 链接关闭过程从客户端 -> 服务器发送所需的时间,都是脚本可以继续运行的时间量。
Without the exit call, the exact point/time at which your script will terminate will come down to two factors:
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.
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.