PHP cache Files


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $cachefile = 'cache.html';
  2. $cachetime = 4 * 60;
  3. // Serve from the cache if it is younger than $cachetime
  4. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
  5. include($cachefile);
  6. echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
  7. }
  8. ob_start(); // Start the output buffer
  9.  
  10. /* Heres where you put your page content */
  11.  
  12. // Cache the contents to a file
  13. $cached = fopen($cacheFile, 'w');
  14. fwrite($cached, ob_get_contents());
  15. fclose($cached);
  16. ob_end_flush(); // Send the output to the browser

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.