Cache Page Script


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



Copy this code and paste it in your HTML
  1. if (isset($useCache) && $useCache==1){
  2. if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI']!="/"){
  3. $cacheName=str_replace('/','-',$_SERVER['REQUEST_URI']);
  4. $cacheName = substr($cacheName,1);
  5. }else{
  6. $cacheName='site-index.html';
  7. }
  8.  
  9. $cacheFile = "cache/{$cacheName}";
  10.  
  11. if(!isset($cacheTime)){
  12. $cacheTime = 4 * 60;
  13. }
  14.  
  15. if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) {
  16. include($cacheFile);
  17. }
  18.  
  19. }
  20.  
  21.  
  22. /************** Body of site **************/
  23.  
  24.  
  25. if (isset($useCache) && $useCache==1){ // Firstly we check, is caching enabled for this page, if yes do your best my script:)
  26. $cached = fopen($cacheFile, 'w');
  27. fwrite($cached, ob_get_contents());
  28. fclose($cached);
  29. ob_end_flush(); // Send the output to the browser
  30. }

URL: http://paste.org/5666

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.