Forum Moderators: coopster
Using PHP 4, I'd like to cache webpages instead of using PHP each time visitors access them.
I've created a webtool showing personal profiles, and they use a lot of SQL queries within each page. I want to create a cached copy each time one user modifies their profile, and I've used the cool idea proposed on this WebmasterWorld thread:
[webmasterworld.com...]
It creates a cached copy of the webpage, and serves it when visitors access it.
Does anybody know any alternative PHP library to carry this out? I created it by myself, but would like to find one which is already developed.
I'm trying to integrate more features, like showing user's nickname if they are logged in. I can insert whithin cached copies some PHP code in order to make Apache interprete it, but I don't know if somebody made it before.
Thank you very much.
Create 2 files, cache_header.php and cache_footer.php. In cache_header.php, add the following:
$filename = "%%-".md5($_SERVER['REQUEST_URI'])."-%%.html";
$cachefile = "/my/cache/directory/".$filename;
$cachetime = 30 * 60;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile))
{
include($cachefile);
exit;
}
ob_start();
Then in cache_footer.php, add the following:
$fp = fopen($cachefile, 'w+');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
It's really simple, just create the two files, include cache_header.php at the start of the page you would like to cache output from, include cache_footer.php at the end of the same page and you are done.