PHP 标头位置甚至在输出缓冲区内也会发送?
我无法从输出缓冲区内抑制 PHP 位置标头。据我了解,输出缓冲区应该抑制标头,直到它们被刷新。我还认为不应使用 ob_end_clean() 发送标头。
但是,如果您看到下面的代码,如果我取消注释标题行(第二行),我总是会被重定向到谷歌并且永远不会看到“完成”。
ob_start();
//header("Location: http://www.google.com");
$output = ob_get_contents();
ob_end_clean();
$headers_sent = headers_sent();
$headers_list = headers_list();
var_dump($headers_sent);
var_dump($headers_list);
die('finished');
我需要抑制任何标头重定向,理想情况下将它们捕获在输出缓冲区中,这样我就知道这些条件会产生重定向。我知道我可以使用curl来做到这一点(将跟随重定向设置为false),但是由于我想要缓冲的所有文件都在我自己的服务器上,curl被证明非常慢并且占用了数据库连接的负载。
有人有任何建议或知道捕获/抑制位置标头的任何方法吗?
谢谢, 汤姆
I am having trouble suppressing a PHP Location Header from inside an output buffer. It is my understanding that output buffers should suppress headers until they are flushed. I also thought that no headers should be sent with ob_end_clean().
However if you see the code below, if I uncomment the header line (second line) I always get redirected to google and never see 'finished'.
ob_start();
//header("Location: http://www.google.com");
$output = ob_get_contents();
ob_end_clean();
$headers_sent = headers_sent();
$headers_list = headers_list();
var_dump($headers_sent);
var_dump($headers_list);
die('finished');
I need to suppress any header redirects, ideally catching them in the output buffer so I know those conditions will produce a redirect. I know I can do this with curl (setting follow redirects to false), but as all the files I want to buffer are on my own server curl is proving very slow and tying up loads of db connections.
Does anyone have any suggestions or know of any way of catching/suppressing Location Headers?
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看是否可以使用
header_remove
与headers_list
一起使用。这似乎适用于 IIS/FastCGI 和 Apache:PS:不管 ob_start 文档怎么说,PHP 在即将发送输出的第一个字节时(或当脚本终止时)都会发送标头。如果没有输出缓冲,您的代码必须在发送任何输出之前操作标头。通过输出缓冲,您可以按照您喜欢的方式交错标头操作和输出,直到刷新缓冲区。
See if you can use
header_remove
function along withheaders_list
. This seemed to work on IIS/FastCGI and Apache:PS: despite of what the ob_start documentation says, PHP will send headers when it is about to send the first byte of output (or when the script is terminates). Without output buffering, your code must manipulate headers before sending any output. With output buffering, you can interleave header manipulation and output anyway you like until you flush the buffer.
如果您阅读 ob_start 的手册页,第一段是:
If you read the manual page for ob_start the first paragraph is:
不:
来源: http://us.php.net/manual/ en/function.ob-start.php
您可以在发送标头之前尝试刷新:
Nope:
Source: http://us.php.net/manual/en/function.ob-start.php
You can try flushing before sending headers though: