Use case for output buffering as the correct solution to "headers already sent"
Asked Answered
H

5

8

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.

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?

Haug answered 27/5, 2010 at 8:34 Comment(0)
C
6

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.

Captain answered 27/5, 2010 at 8:40 Comment(0)
D
2

The only situation I can imagine is a CMS or Weblog in which plugins can be invoked in the HTML code, like

<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. 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.

Drub answered 27/5, 2010 at 8:58 Comment(3)
I think you are talking about a different issue. HTML <head> is different from HTTP header.Manheim
@ZZ Coder nope, I'm talking about inserting code in a place that is "above" the place you are processing at the moment. Whether that is a different position within the <body> or the <head> element doesn't really matter. The point is about using buffering to modify the output before it is sent.Drub
@Pekka웃 Which is not really what the question was about.Erroneous
B
0

for template systems you will need ob_start ... look and Zend_View

Later Edit I misunderstood the question and provided a case where ob_start use is a valid solution.

Borrowing answered 27/5, 2010 at 8:51 Comment(2)
True, template systems need it. The question regards "Headers already sent" situations though.Embower
solomongaby is implying that some templating systems use output buffering to render fragments of the template to be merged at some later step of rendering. However, while this is a valid use of output buffering, it is not an explanation why buffering might be valid way to solve the "headers" problem which is the original question.Captain
W
0

You might want to issue HTTP redirects late in the flow, for example in templates or exception handling. (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.)

Wham answered 27/5, 2010 at 9:19 Comment(0)
J
0

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

2) there are other uses for output_buffering other than fixing the 'Headers already sent' message - e.g. compressing output, capturing output of arbitary code, avoiding chunked encoding....

C.

Jhelum answered 27/5, 2010 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.