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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 5 stars5 stars5 stars5 stars5 stars / 48
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · The article deals with the basics of PHP Output Caching. Also, it shows some chunked...
       · For the sake of clarity and accuracy, where it reads $cachefile, should be...
       · Too much editorialising, discussing history, something. After reading the first...
       · Hello Sr.Thank you for the comment. I'm sorry you didn't enjoy the article. That...
       · I find it interesting that you didn't mention Smarty as a caching option. On top of...
       · Hi David,Thank you for the commnent. The interspered PHP is only for the...
       · i am a beginner, just pure questions.what's the point of using all the ob_start,...
       · Yes , i like it and am using it long time ago , the benefit( my opinion) the cache...
       · Hi,I totally agree with you!Alejandro Gervasio
       · Hello, This article is intented to be an introduction to PHP cache capabilities,...
       · I disagree, the beginning of the article made the subject and benefit quite...
       · David says he'd like to see the HTML and PHP in separate files. I've been reading a...
       · Well, Smarty is an excellent template system (and includes more capabilities too)...
       · im kinda new to caching and I was trying the original script..im sorry if it looks...
       · ok im replying to myself, erm it's late here and i hate these kinda glitches that...
       · Is it possible to implement this process in the phpBB code for discussion...
       · Thank you for posting your comment here. With regard to your question, unfortunately...
       · Thanks for the very prompt reply! I guess I should just give up then....have spent...
       · I'd like thank you for posting your feedback here. With reference to your problem,...
       · Good Tutorial thk,just a little correct thoughIt would probly not be long for U...
       · Hi Sig again,Thank you fo pointing out the missing parenthesis on the code. It...
       · thnX a lot Alejandro for this amazing article.when I was read it ,I could see the...
       · Thank you for the kind comments on my PHP article and your excellent suggestion on...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway