PHP
  Home arrow PHP arrow Page 3 - 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 - Output Buffering for Server Side Caching
    ( Page 3 of 5 )

    Now, with this example in our hands, let’s see how we can use output buffering for server side caching. Let's implement a very simple demonstration, where the buffer content is stored as a file:


    <?php

    //   check if there is a cached version
    if ( file_exists( ‘cachefile.txt’ ) ) {
       //   if there is a cached version read content and display
      readfile ( ‘cachefile.txt’ );
      exit();
    }

    //   if there is not a cached version start output buffering
    ob_start();

    //   display some HTML ( this will be stored in the buffer )
    ?>

    <html>
    <head>
    <title>Caching server output</title>
    </head>
    <body>
    <h2>This page is a cached Page</h2>
    </body>
    </html>

    <?php
    $bufferContent = ob_get_contents();
       //   get buffer content
    ob_end_flush();
       //   clean and display buffer content in the browser
    $fp = fopen ( ‘cachefile.txt’ , ‘w’ ) or die ( ‘Error opening cache file’ );
       //  write buffer content to cache file
    fwrite ( $fp , $bufferContent );
    fclose( $fp );
    ?>

    The output for the above script is the parsed HTML:

    This page is a cached Page

    The contents of the cache file ‘cachefile.txt’ is the same HTML included in the script:

    <html>
    <head>
    <title>Caching server output</title>
    </head>
    <body>
    <h2>This page is a cached Page</h2>
    </body>
    </html>

    Let’s examine in detail what’s going on within the script:

    First, the script checks to see if there is a cached version of the page, by determining if a cache file exists. If it does, it then reads the cache file and send the output to the browser. Otherwise, it starts a new output buffer and store some HTML on it to generate the cached version. Once this is done, it gets the buffer content and stores it in a variable ($bufferContent). Next, the buffer is cleared and the HTML is displayed to the user. Finally, the buffer content is written to the cache file.

    Obviously, this script is very simple, but exposes in a nutshell the powerful caching capabilities that PHP has built-in, when used in conjunction with output buffering functions. Having all of this potential in our hands, the logical next step is caching different sections of a Web page. Web pages usually have a header section, body and a footer section. It would be good to apply these caching techniques accordingly to how frequently those generic sections are updated.  We’re moving that way now.



     
     
    >>> 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 5 hosted by Hostway
    Stay green...Green IT