0

i have read a bit around in the internet about the php cache. at the moment i am using, this system to cache my pages:

This is putted on the start of the page

<?php // Settings $cachedir = 'cache/'; // Directory to cache files in (keep outside web root) $cachetime = 600; // Seconds to cache files for $cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt) // Ignore List $ignore_list = array( 'addedbytes.com/rss.php', 'addedbytes.com/search/' ); // Script $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page $cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create $ignore_page = false; for ($i = 0; $i < count($ignore_list); $i++) { $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page; } $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0; @clearstatcache(); // Show file from cache if still valid if (time() - $cachetime < $cachefile_created) { //ob_start('ob_gzhandler'); @readfile($cachefile); //ob_end_flush(); exit(); } // If we're still here, we need to generate a cache file ob_start(); ?> 

MY HTML CODE Goes here .............

and the code below is at the footer of my page.

<?php // Now the script has run, generate a new cache file $fp = @fopen($cachefile, 'w'); // save the contents of output buffer to the file @fwrite($fp, ob_get_contents()); @fclose($fp); ob_end_flush(); ?> 

There are some things that i need and this code dont have them :

  • gzip
  • the expired cache is not autodeleted after it expire.

Also wanted to ask, if this code is secure to use , if some one can suggest a better one or something to improve the current code it will be just great

Thank you fro reading this post. Best Regards Meo

3 Answers 3

2
…. // Show file from cache if still valid if (time() - $cachetime < $cachefile_created) { //ob_start('ob_gzhandler'); echo gzuncompress(file_get_contents($cachefile)); //ob_end_flush(); exit(); } else { if(file_exists($cachefile) && is_writable($cachefile)) unlink($cachefile) } …. 

and

 // Now the script has run, generate a new cache file $fp = @fopen($cachefile, 'w'); // save the contents of output buffer to the file @fwrite($fp, gzcompress(ob_get_contents(), 9)); @fclose($fp); ob_end_flush(); ?> 
Sign up to request clarification or add additional context in comments.

8 Comments

please can you show me how ? any kind of example, i am not an expert on this things. thank you
ok thank you, so it will automaticly gzip the files ? in my cache folder the files are not gziped.. is this normal ?
sorry missunderstood your question.. posted another solution
nvm fixed, it was an error at this row: @fwrite($fp, gzcompress(ob_get_contents(), 9))
typo.. there was a missing ) after the fwrite @fwrite($fp, gzcompress(ob_get_contents(), 9));
|
1

Use ob_start("ob_gzhandler"); to initiate gzipped buffering (it'll take care of determining if the client can actually accept/wants gzipped data and adjust things accordingly).

To delete the cached files:

if (time() - $cachetime < $cachefile_created) { @readfile($cachefile); //ob_end_flush(); exit(); } else { unlink($cachefile); exit(); } 

3 Comments

about the ob_start("ob_gzhandler"); i see that its commented on my code, so this is everything i need to make my files gzip ? should i add smth else ? in .htaccess ? thnx
i have tryed to add the unlink thing u said to me, and i get this error, Warning: unlink(cache/fd79b70952042a73c1e9b2e65280cfa1.html) [function.unlink]: No such file or directory in D:\database\xampp\htdocs\index.php on line 44
I'd suggest removing all the @ error suppressions. They're a BAD thing to have while developing/debugging - hiding problems is never a good idea... especially while developing.
0

But there can be delay or maybe error when the file is being written and someone requests for that page. You should use flock to overcome such problems as mentioned at Error during file write in simple PHP caching

Something like this at the end of page

<?php $fp = @fopen($cachefile, 'w'); if (flock($fp, LOCK_EX | LOCK_NB)) { fwrite($fp, gzcompress(ob_get_contents(), 9)); flock($fp, LOCK_UN); fclose($fp); } ob_end_flush(); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.