PHP
  Home arrow PHP arrow Page 5 - 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 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 / 49
    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 - Putting it All Together


    (Page 5 of 5 )

    Here is the complete script:

    <?php

    //  define createCache
    function createCache ( $content ,  $cacheFile ) {
      $fp = fopen( $cachefile , ‘w’ );
      fwrite( $fp , $content );
      fclose( $fp);
    }

    // define getCache
    function getCache ( $cacheFile ,  $expireTime ) {
      if ( file_exists ( $cacheFile ) && filemtime ( $cacheFile ) >( time() - $expireTime ) ) {
        return file_get_contents( $cacheFile );
      }
      return false;
    }

    //   start output buffering
    ob_start();

    //  check if a valid header cache exists
    if ( !$header = getCache( ‘headerCache.txt’ , 86400 ) {

    //  display header section

    ?>

    <html>
    <head>
    <title>Cached Page</title>
    </head>
    <body>
    The header section is updated on a daily basis.

    <?php
    $header = ob_get_contents();
    ob_clean();
    createCache( $header , ‘headerCache.txt’ );

    }

    // check if a valid body cache exists
    if ( !$body = getCache( ‘bodyCache.txt’ , 10 ) {

    //  display body section
    ?>

    <h1>This section is updated every 10 seconds.</h1>


    <?php
    $body = ob_get_contents();
    ob_clean();
    createCache( $body , ‘bodyCache.txt’ );

    }

    // check if a valid footer cache exists
    if ( !$footer = getCache( ‘footerCache.txt’ , 86400 ) {

    //  display footer section
    ?>
    The footer section is updated on a daily basis.
    </body>
    </html>

    <?php
    $footer = ob_get_contents();
    ob_clean();
    createCache( $footer , ‘footerCache.txt’ );

    }

    //   stops output buffering
    ob_end_clean();

    //   display the complete page

    echo $header .  $body .  $footer;

    ?>

    Conclusion

    That’s the general idea behind the concept of caching server side output. Major performance improvements can be achieved if used in conjunction with proper caching policies for specific sections of the website.

    Since I am a strong advocate of object oriented programming, I would recommend using some good and trusted caching classes, such as Pear::Cache_Lite, in order keep your code maintainable and have a reliable caching mechanism for websites.

    From this point, there is long way to go. Caching is a very huge subject, and it can be approached from several points. But one thing is certain: caching server output with PHP output buffering functions is a good addition to your toolbox when building dynamic websites. Good luck!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - 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...




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