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.
| | Discuss Output Caching with PHP | | | | | | | 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 though
It 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,... | | | | | | Thanks for your article. This gave me a good view of how Caching works, had no idea... | | | | | | >>> Post your comment now! | | | | | |
|
 |