Simple page caching

I will give an example of a simple caching of PHP pages with an interval of 600 seconds (10 minutes).

Paste at the beginning of the code:

<?php
$url=$GLOBALS['REQUEST_URI'];
$crc=md5($url);
$modif=time()-@filemtime ("cache/$crc");
if ($modif<600)
{
include ("cache/$crc");
exit();
}
ob_start ();
?>

The code for the page itself:

<html>
...
</html>

Paste at the end of the code:

<?php
$cache = ob_get_contents();
ob_end_clean ();
echo $cache;
$fp = @fopen ("cache/$crc", "w");
@fwrite ($fp, $cache);
@fclose ($fp);
?>

We will also create the “cache” folder, after opening the page a cache will be written to it.

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a comment

Leave a Reply