How can an output buffer worsen performance
Asked Answered
T

4

3

I am writing a php script and somewhere before my header() function i have printed text to the browser hereby causing my header() function give me a well known error:

Warning: Cannot modify header information - headers already sent.

Now my question is, I have the intentions of using ob_start() and ob_flush() before and after the header() function. But I once heard something like output buffer can affect the performance of one's application negatively. How true is that?

Or should I just stick with the idea of printing Javascript function to redirect the page.

Thanks for your time.

Troop answered 8/2, 2009 at 20:53 Comment(1)
I thank you all very much for your time and positive contributions to my question.now i understand things better, most especially the output buffer that got me confused for a while. After all the posts, i have been able to come with the right approach.thanks guys, you all saved me a lot of thinking.Troop
S
4

Using an output buffer requires the server to store the entire output of the PHP in RAM, so if it's a large page, you'll wind up using a fair amount of memory - and the server will also have to wait until the entire page is generated before sending it out, which could cause a small delay. But other than that, I don't think there's much of a disadvantage in using an output buffer. For what you want to do, it's certainly a reasonable solution.

Sybaris answered 8/2, 2009 at 20:57 Comment(2)
+1 for pointing out that the data isn't streamed until the ob_flush() is called, which will cause slower load times (but not necessary slower generation times).Floruit
I have a question about the topic, can I what with you here ?Instructor
F
5

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Test ob_start and friends to see if the performance difference matters. If it does, look for alternatives.

The easiest option is to move the header() call before your printing.

As you are likely performing a redirect with something like:

header('Location: /new/location/');

You should not print anything before this header() call because the client wouldn't do anything with the data you printed anyway (unless there's something I'm missing about HTTP).

(Javascript is not a good option for redirects, and nor is meta refreshing, unless you want to detect Javascript for some reason.)

Floruit answered 8/2, 2009 at 20:56 Comment(0)
S
4

Using an output buffer requires the server to store the entire output of the PHP in RAM, so if it's a large page, you'll wind up using a fair amount of memory - and the server will also have to wait until the entire page is generated before sending it out, which could cause a small delay. But other than that, I don't think there's much of a disadvantage in using an output buffer. For what you want to do, it's certainly a reasonable solution.

Sybaris answered 8/2, 2009 at 20:57 Comment(2)
+1 for pointing out that the data isn't streamed until the ob_flush() is called, which will cause slower load times (but not necessary slower generation times).Floruit
I have a question about the topic, can I what with you here ?Instructor
D
2

Relocating in PHP-code after output can say about bad application design. But I don't know your situation and can propose two possible ways.

  1. Split code into model (data processing) and view (output) (see MVC). This means that you are making decision about relocating even before displaying anything. I'd called this way preferred.
  2. If you really need to show output (or other headers sent), common way is combining JS and HTML (in noscript):

    if (headers_sent()) {
        print('<script type="text/javascript">( document.location.replace ) ? document.location.replace("'.$location.'") : document.location.href = "'.$location.'";</script>'."\n".'<noscript><meta http-equiv="Refresh" content="0;URL='.$location.'" /></noscript>');
    } else {
        header('Location: '.$location);
        exit;
    }
    

P.S. This code is a piece of Fusebox framework.

Dareece answered 8/2, 2009 at 21:19 Comment(3)
thanks for the advise. should have applied the MVC technique right from the start of the project.apart from zend framework,which other framework can u recommend? i mean one that will be easy to use.Troop
Actually, most of popular frameworks force you to use MVC in some form. Also they'll give you a lot of other bonuses, e.g. clean app structure, ORM etc. Examples: cakephp.org is good but needs some learning to start using, codeigniter.com seems to be easier to start.Dareece
Oh, and the most important thing imo: even if you'll spend some time now to transfer your existing dev code into framework -- you'll save it back in future.Dareece
I
0

just answering your last remark: you can redirect the page in php using header('Location: '.$url) it should go before any other output obviously and is advised to be followed by exit();

Incalescent answered 8/2, 2009 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.