Presentation on theme: "Output Buffering Control with php By Tim LeClair Senior Web Developer, Skills Gatekey Corporation."— Presentation transcript:
Output Buffering Control with php By Tim LeClair Senior Web Developer, Skills Gatekey Corporation
Who is this presentation for? Web Developers / Programmers Output buffering is a unique tool that allows for creative ways to make things happen and provide more options. Web Designers IT Project managers Web Designers, It Managers, in creating better websites, understanding an overview of the powers that web technologies provide will allow you to create to your fullest extent. Owners of websites that push / plan to push the limits Before you expand hardware wise, it is important to tweak your websites efficiency prior or your cost and complexity will multiply quickly. Knowledge to help Intelligence create something new
Why use Output Buffers? More control More speed Less bandwidth Less Database connections Reduce redundant script execution Put your html in a file or variable Tune / manipulate your output Lesson objectives Gains in one can equal loss in another!
Output Control Functions ob_start(callback function) Turn on output buffering Initialize / Functions / Exiting output_add_rewrite_var Will also turn on the output buffer
Output Control Functions ob_end_clean Clean (erase) the output buffer and turn off output buffering ob_end_flush Flush (send) the output buffer and turn off output buffering ob_get_clean Get current buffer contents and delete current output buffer ob_get_flush Flush the output buffer, return it as a string and turn off output buffering Initialize / Functions / Exiting
Output Control Functions Initialize / Functions / Exiting ob_flush Flush (send) the output buffer ob_clean Clean (erase) the output buffer ob_get_contents Return the contents of the output buffer ob_implicit_flush Turn implicit flush on/off flush Flush the output buffer
Output Control Functions Initialize / Functions / Exiting ob_get_length Return the length of the output buffer ob_get_level Return the nesting level of the output buffering mechanism ob_get_status Get status of output buffers ob_list_handlers List all output handlers in use output_add_rewrite_var Add URL rewriter values output_reset_rewrite_vars Reset URL rewriter values ob_iconv_handler() Convert character encoding as output buffer handler ob_tidyhandler() ob_start callback function to repair the buffer (requires libtidy installation)
Compression options Web server Level mod_deflate (apache 2) mod_gzip Apache.conf.htaccess Script level Zlib.output.compression Output buffer function gzhandler Php.ini Php script Remember rule: compressing twice is a bad thing! This is not a lesson on compression but need to know to prevent conflict.
Gzip and Output Buffers ob_start(ob_gzhandler) requires the zlib extension. You cannot use both ob_gzhandler and zlib.output compression. Gzip + Gzip + unGzip = TRASH Differences Website tester for compression at:
Zlib extension Zlib support in PHP is not enabled by default. You will need to configure PHP The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions. Note: Built-in support for zlib on Windows is available with PHP XAMPP for Linux The distribution for Linux systems (tested for SuSE, RedHat, Mandrake and Debian) contains: Apache, MySQL, PHP & PEAR, Perl, ProFTPD, phpMyAdmin, OpenSSL, GD, Freetype2, libjpeg, libpng, gdbm, zlib, expat, Sablotron, libxml, Ming, Webalizer, pdf class, ncurses, mod_perl, FreeTDS, gettext, mcrypt, mhash, eAccelerator, SQLite and IMAP C-Client. XAMPP for Windows The distribution for Windows 98, NT, 2000, 2003, XP and Vista. This version contains: Apache, MySQL, PHP + PEAR, Perl, mod_php, mod_perl, mod_ssl, OpenSSL, phpMyAdmin, Webalizer, Mercury Mail Transport System for Win32 and NetWare Systems v3.32, Ming, JpGraph, FileZilla FTP Server, mcrypt, eAccelerator, SQLite, and WEB-DAV + mod_auth_mysql. phpinfo();
HTTP compression Most content is cached by the users browser. Dynamic content in the HTML code is not cacheable as it changes, so HTTP compression can lessen the bandwidth and time to 1/3 to 1/5 easily. That is a big savings! Yslow On Firefox with Firebug SmartSniff Without compression Twitter With compression
Using callback functions Getting Started
Exiting output buffers Easiest way Just let the script end ! Flush to lower buffer level and end current level Erase and end current buffer level Almost as easy
Buffer Levels Level 1 Level 2 Level 3 Level 4 Server Level 3 Output to Browser Ob_start (ob_gzhandler) Ob_start (condense) Ob_start (ob_tidyhandler) Ob_start 20 Db calls cache.php If(cache.php ) readfile(cache.php) else Level 4 Tidy it up Remove junk Compression Ob_start (4heckofit) Header Top Right Menu Advertisement Left Ads BLOG HeaderTop Right Menu Advertisement BLOG Left Ads Bottom
Project 1a - Output buffering
Project 2a – Nesting 1452Answer =
Project 2b – Nesting 12452Answer =
Project 2c – Nesting Answer =
Project 3a – Other functions 0- length 2- level 25- length 1- level Level 0 = no buffer
Project 6 – Less HTML less readable <", $buffer); // remove single spaces around $buffer = str_replace(" ", " ", $buffer); return $buffer; } ob_start(" condense "); // your website ob_end_flush(); ?>
Project 7 – Tidyhandler test Uncomment in php.ini 4 xampp ;extension=php_sybase_ct.dll ;extension=php_threads.dll extension=php_tidy.dll ;extension=php_timezonedb.dll ;extension=php_translit.dll
Project 8 – Compression level
Bug reports Search terms like ob_start Output buffers can effect the whole document, so it is wise to test all aspects of your website for functionality and with various browsers.
WIMP issues Example: ORIGINAL CHANGE You MUST add responseBufferLimit="0 By default, IIS buffers everything up to a massive 4MB This lecture was written with Apache as the example server. If you are using IIS then it is recommended to look closely at how IIS handles buffers and compression.
Summary More control More speed Less bandwidth Less MySQL connections Reduce redundant script execution Put your html in a file or variable Tune your output Did lesson meet its intended objectives
Questions Sorry, we do not answer questions If you come across this document online, please refer to or use a search engine to find your answers. Else if you catch me standing in front of you, then I will do my best to answer your questions.www.php.net
The End Acknowledgments: Rasmus Lerdorf creator of PHP Mehdi Achour, Friedhelm Betz, Antony Dovgal, Nuno Lopes, Hannes Magnusson, Georg Richter, Damien Seguy and Jakub Vrana noted active contributors of PHP