简体   繁体   English

用于输出缓冲的用例是“已发送标头”的正确解决方案

[英]Use case for output buffering as the correct solution to “headers already sent”

I see (not just on this site) a lot of question from inexperienced PHP programmers about the infamous "headers already sent... output started at" error, and many people suggest using ouput buffering as a solution. 我看到(不仅仅是在这个网站上)来自没有经验的PHP程序员关于臭名昭着的“标题已经发送...输出开始于”错误的很多问题,并且许多人建议使用输出缓冲作为解决方案。

In my experience I have never found a situation where that error wasn't caused by a flaw in the program's logic. 根据我的经验,我从未发现错误不是由程序逻辑中的缺陷引起的。 Are there cases where output buffering is actually the correct solution? 是否存在输出缓冲实际上是正确解决方案的情况?

I would concur with your initial statement. 我同意你的初步陈述。 Generally, solving "headers" problem with output buffering is a stopgap measure. 通常,解决输出缓冲的“标题”问题是权宜之计。

The really sad/funny part of this solution is: what happens when you want to output something large, such as a file you are keeping behind a paywall? 这个解决方案真正令人伤心/有趣的部分是:当你想要输出一些大的东西时会发生什么,例如你在付费墙后面的文件? Usually it results in people replacing the "headers" problem with their scripts running out of memory. 通常它会导致人们用脚本耗尽内存来替换“头”问题。

Whoops. 哎呦。

The only situation I can imagine is a CMS or Weblog in which plugins can be invoked in the HTML code, like 我能想象的唯一情况是CMS或Weblog,其中可以在HTML代码中调用插件,例如

<h1>My images</h1>
{plugin:show_images}

those plugins may have to add their own style sheets and other things that go in the <head> section of the page. 这些插件可能必须添加自己的样式表和页面<head>部分中的其他内容。 Using buffering, this would be possible. 使用缓冲,这是可能的。

In practice though, this is not good for performance, feels kludgy and doesn't work when output buffering is turned off. 但在实践中,这对于性能不利,感觉很糟糕,并且在关闭输出缓冲时不起作用。 Even here, it is therefore better to pre-process the contents before showing them, and doing any adding of style sheets etc. before anything is output. 即使在这里,因此最好在显示内容之前预处理内容,并在输出任何内容之前进行任何样式表等的添加。

for template systems you will need ob_start ... look and Zend_View 对于模板系统,您将需要ob_start ... look和Zend_View

Later Edit I misunderstood the question and provided a case where ob_start use is a valid solution. 后来编辑我误解了这个问题并提供了ob_start使用是一个有效解决方案的案例。

You might want to issue HTTP redirects late in the flow, for example in templates or exception handling. 您可能希望在流程后期发布HTTP重定向,例如在模板或异常处理中。 (Of course, a framework with templating or global exception handling would need output buffering anyway, so you could say it isn't a solution to this problem specifically.) (当然,具有模板化或全局异常处理的框架无论如何都需要输出缓冲,所以你可以说它不是解决这个问题的具体方法。)

In my experience I have never found a situation where that error wasn't caused by a flow in the program's logic. 根据我的经验,我从未发现错误不是由程序逻辑中的流程引起的。 Are there cases where output buffering is actually the correct solution? 是否存在输出缓冲实际上是正确解决方案的情况?

I'd have to agree with you, however: 不过,我不得不同意你的看法:

1) One of the reasons I like PHP is because it lets you choose how you solve the problem 1)我喜欢PHP的一个原因是因为它可以让你选择如何解决问题

2) there are other uses for output_buffering other than fixing the 'Headers already sent' message - eg compressing output, capturing output of arbitary code, avoiding chunked encoding.... 2)除了修复“Headers already sent”消息之外,还有output_buffering的其他用途 - 例如压缩输出,捕获任意代码的输出,避免分块编码....

C. C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM