php header 立即定位?
为什么 Location 标头没有立即重定向页面? 它总是在重定向之前执行整个过程?
我将举一些例子:
header('Location:http://www.php.net');
$f = fopen('demo.txt','w+');
fwrite($f,'Test');
fclose($f);
它总是在重定向到 http://www.php.net
之前生成 TXT 文件。
Why the Location header does not redirect the page immediately?
It always performs the entire process before redirecting?
I will give some example:
header('Location:http://www.php.net');
$f = fopen('demo.txt','w+');
fwrite($f,'Test');
fclose($f);
It always generates the TXT file before redirecting to http://www.php.net
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,
header()
只是将某些标头发送到浏览器。此后 PHP 仍继续运行该脚本。如果您不想停止运行脚本,只需使用die;
或exit;
- 它将停止进一步处理脚本。Well,
header()
just sends certain header to browser. PHP still continues running the script after that. If you don't want to stop running the script, just usedie;
orexit;
- it will stop processing the script further.这是因为
header()
函数不重定向,它发送一个标头。浏览器可能(理论上)完全忽略它并选择继续解析页面。如果您希望脚本在发送标头后不进行处理,请立即触发die()
或exit()
。It's because the
header()
function does not redirect, it sends a header. A browser may (theoretically) completely ignore it and choose to continue parsing the page. If you wish the script to not process after sending the header, firedie()
orexit()
immediately after.只是因为它根本不重定向任何内容。浏览器将中断当前连接(导致脚本停止)以请求另一个页面。并且存在网络延迟。
并非总是如此。只是无法保证。
使用 mod_php 生成日志,您将需要
ignore_user_abort()
,而使用 php-fpm 则需要fastcgi_finish_request() 以保证整个脚本执行。
Just because it doesn't redirect anything at all. It's browser who will brake current connection (causing script to stop) to requests another page. And there is a network latency.
Not always. It's just not guaranteed.
with mod_php you will need
ignore_user_abort()
and with php-fpm it'sfastcgi_finish_request()
to guarantee entire script execution.