PHP Output Buffering

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    PHP Output Buffering - Presentation Transcript

    1. PHP OUTPUT BUFFERING PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    2. “The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.” Source: http://www.php.net/manual/en/intro.outcontrol.php PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    3. How often has this happened to you? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    4. Warning: Cannot modify header information - headers already sent by (output started at /path/to/script.php:123) in /path/to/ otherscript.php on line 321 PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    5. What about this? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    6. (Don’t worry, it’s fake. Yahoo!, please don’t sue.) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    7. Content might not float correctly if it isn’t all there yet. (This happened a lot more on dial-up) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    8. Output buffering fixes both of these common issues. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    9. ENABLE OUTPUT BUFFERING (PHP.INI VERSION) • output_buffering = On • output_buffering = 4096 (bytes) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    10. Buffers output. Fixes both problems. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    11. But there has to be more. Or else this would be a lame presentation. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    12. ENABLE OUTPUT BUFFERING (CODE VERSION) • ob_start(); // Start a new output buffer • ob_flush(); // Send contents to browser • ob_end_flush(); // Send contents to browser, destroy buffer • ob_clean(); // Discard buffer contents • ob_end_clean(); // Discard buffer contents & destroy buffer • ob_get_contents(); // Contents of the current buffer PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    13. ob_start • bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] ) • Optional $output_callback routine called before outputting or discarding buffer. Gets copy of buffer (string) and must return string. • Optional $chunk_size (size of the buffer) • Optional $erase (false == don’t destroy buffer until script ends) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    14. Call ob_start() at the top of your script same as output_buffering = On in php.ini PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    15. CACHING • At script’s start, see if there’s a cached copy of the output • If not, continue with the script • Use ob_get_contents() or $output_callback to get buffer contents and cache it • Next time script runs, it’ll find the cached copy PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    16. MORE INFO ON CACHING http://www.slideshare.net/csixty4/caching-data-for-performance PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    17. MODIFY THE BUFFER • ob_get_contents() or $output_callback again • Get the buffer as a string • Do str_replace, maybe parse it as XML • Return a string version PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    18. MODIFY THE BUFFER (WHAT CAN YOU DO WITH IT?) • Templating system • Form building library • Unicode character replacement (accents, fancy quotes) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    19. (At this point, Mike Creuzer spent a few minutes talking about a script he wrote that calls a Twitter library and uses an output buffer to capture the results, then pulls what he wants out of the output.) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    20. NESTING BUFFERS • Call ob_start() as many times as you want • Useful for capturing output from libraries as strings PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    21. NESTING BUFFERS ob_start(); echo “stuff”; ob_start(); // create a new buffer generateATonOfHTML(); $buff = ob_get_contents(); ob_end_clean(); $buff = str_replace(‘Dog’, ‘Cat’, $buff); echo $buff; PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    22. CAVEAT You knew there had to be a catch PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    23. If output buffering is so good, why isn’t it turned on by default? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    24. BUFFERS TAKE UP MEMORY • If set to “On” or ob_start() called w/o $chunk_size, uses as much RAM as needed • Ifset to a number or ob_start() called with $chunk_size, uses that much memory • Memory use multiplied by # of nested buffers • Each simultaneous script needs RAM PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    25. Your script could run out of memory. PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    26. Your server could run out of RAM and have to swap (sloooooooooooow) PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    27. (Fairly) easy to mitigate: buy RAM and increase memory_limit in php.ini PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009
    28. Questions? PHP Output Buffering :: David Ross :: Suburban Chicago PHP & Web Development Meetup :: suburbanchicagophp.org :: August 6, 2009

    + Dave RossDave Ross, 3 months ago

    custom

    792 views, 0 favs, 1 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 792
      • 787 on SlideShare
      • 5 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 5
    Most viewed embeds
    • 5 views on http://suburbanchicagophp.org

    more

    All embeds
    • 5 views on http://suburbanchicagophp.org

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories