Home arrow PHP arrow Page 2 - Output Caching with PHP

Capturing Server Side Output - 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 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:

   // 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
   //   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

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: