PHP
  Home arrow PHP arrow Page 2 - Output Caching with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Output Caching with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 55
    2005-01-11


    Table of Contents:
  • Output Caching with PHP
  • Capturing Server Side Output
  • Output Buffering for Server Side Caching
  • Multiple caching: splitting the Web page content
  • Putting it All Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Output Caching with PHP - Capturing Server Side Output
    ( Page 2 of 5 )

    Now, with the preliminaries out of the way, let’s see how server side delay can be reduced using PHP. We will generate a normal PHP page, maybe retrieving records from a database, performing others tasks needed for the page, and so on. However, before sending the final rendered page to the browser, we will need to capture that output and store it in a file (the cache file). The next time the page is requested, we’ll find out whether there is a cached version of it (this means checking to see if the cache file exists).

    If there is a cache file, instead of rebuilding the entire page again, we’ll just read the contents from the file and send them straight to the browser. This will noticeably reduce the time taken to redisplay the page. In order to catch the server output, we will utilize some of the buffer control functions that PHP offers for storing data in a buffer created in server memory. Buffer control functions (when used properly) offer a great mechanism for controling server output, whether output is cached or processed in different ways (for instance, when building pages from template systems).

    Here is a simple example of output buffering:

    <?php
    ob_start();
       // start an output buffer
    echo ‘This text is in the buffer!<br />’;
       // echo some output that will be stored in the buffer
    $bufferContent = ob_get_contents();
       // store buffer content in a variable
    ob_end_clean();
       //   stop and clean the output buffer
    echo ‘This text is not in the buffer!<br />’;
       //   echo text normally
    echo $bufferContent;
       //   echo the buffer content
    ?>

    As we can see clearly, the above script starts output buffering with the function ob_start. Then it uses echo to display some text, which is stored in the buffer. Next, it retrieves the buffer content and stores it in a variable ($bufferContent). The ob_end_clean function stops output buffering and cleans the buffer. After that, some text is normally echoed to the browser and finally the buffer content is displayed.

    The script output is the following:

    This text is not in the buffer!
    This text is in the buffer!

    This simplistic example shows us how to capture content from the output buffer, process it in some way (for caching purposes, for example), and finally display output in the browser.

    Think about how powerful this technique could be when building websites. It’s not only possible to manipulate data for caching, but also for filling data into templates placeholders, or to trigger error handling processes, among others advantages. Because we are manipulating data without sending anything to the user’s browser, we can programmatically handle any output, hiding the intermediate processes from the visitor, and behaving according to our application logic.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT