Skip to content

Instantly share code, notes, and snippets.

@paulund
Created June 25, 2013 18:27
Show Gist options
  • Save paulund/5861002 to your computer and use it in GitHub Desktop.
Save paulund/5861002 to your computer and use it in GitHub Desktop.
Here is a quick code snippet which will allow you to easily cache pages in PHP.
<?php
//cache file
$cachefile = 'cached/'.date('M-d-Y').'.php';
//Total time the file will be cached in seconds set to 10 hours
$cachetime = 36000;
//If the cache file already exists and the cache file is over 10hours old then display the cache file
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
//Start the buffer
ob_start();
?>
<html>
outputs everything in the html tag
</html>
<?php
//Opens cache file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment