PHP
  Home arrow PHP arrow Page 2 - Output Caching with PHP
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 / 51
    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


    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


       · 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...
       · There's a straight forward explanation why there must be files. Caching is supposed...
       · Thanks for the great contribution. That’s exactly the purpose in caching PHP...
       · Hey, this is a great article, really made caching seem a simple affair! I am...
       · Thanks for the kind words on my PHP caching article. Regarding your questions,...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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