Home arrow PHP arrow Page 3 - Output Caching with PHP

Output Buffering for Server Side Caching - PHP

Dynamic Web pages take longer to load than static ones, which forces visitors to your site to wait -- and we all know they won't wait for very long. Output caching is a powerful technique you can use to shorten that wait and keep them from leaving.

  1. Output Caching with PHP
  2. Capturing Server Side Output
  3. Output Buffering for Server Side Caching
  4. Multiple caching: splitting the Web page content
  5. Putting it All Together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 64
January 11, 2005

print this article



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:


//   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’ );

//   if there is not a cached version start output buffering

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

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

$bufferContent = ob_get_contents();
   //   get buffer content
   //   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:

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

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: