<?xml version="1.0"?><rss version="2.0"><channel><title>Comments on Caching output in PHP - AddedBytes.com</title><link>http://www.addedbytes.com/article/caching-output-in-php/</link><description>Latest comments on Caching output in PHP on AddedBytes.com</description><!-- ckey="76C662BB" --><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by Casper Helenius ( &lt;a href=""&gt;&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Glen,&lt;br /&gt;
&lt;br /&gt;
Not likely. Google Adsense is actually a piece of javascript, that fetches the content. Thus, the content could - theoretically - be unique everytime - even across caches.&lt;br /&gt;
&lt;br /&gt;
No worries, you can easily use this script in conjunction with Google Adsense. At least, I cannot see what should block it from working.&lt;br /&gt;
&lt;br /&gt;
Please share your experiences here if you conclude something different :)&lt;br /&gt;
&lt;br /&gt;
//Casper</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by Glen ( &lt;a href="http://www.pricelesspartner.com"&gt;http://www.pricelesspartner.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Hi, great script.&lt;br /&gt;
&lt;br /&gt;
Does it cache Google Adsense banners?&lt;br /&gt;
&lt;br /&gt;
Glen</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by Ojuicer ( &lt;a href="http://forumdeli.com/"&gt;http://forumdeli.com/&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Ack, I need to correct myself because actually without the LOCK_NB flag, flock will block until it can get an exclusive lock. Not the behaviour expected judging by the comment.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by Ojuicer ( &lt;a href="http://forumdeli.com/"&gt;http://forumdeli.com/&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;If I could draw your attention to this article &quot;Better Php Caching&quot;.&lt;br /&gt;
http://www.oateck.com/blogs/programming_tips/archive/2008/02/19/php-caching-for-high-traffic-sites.aspx&lt;br /&gt;
(Comments are turned off so I thought it would benefit the discussion here)&lt;br /&gt;
&lt;br /&gt;
With not much more code, the author attempts to greatly improve concurrency. (But fails)&lt;br /&gt;
&lt;br /&gt;
The new logic includes:&lt;br /&gt;
-------------------&lt;br /&gt;
if (file_exists($cfile ) &amp;&amp; (time() - $cachetime&lt; filemtime($cfile )))&lt;br /&gt;
{&lt;br /&gt;
   //if cache is not expired return cache files&lt;br /&gt;
   include($cfile );&lt;br /&gt;
   exit;&lt;br /&gt;
} else{&lt;br /&gt;
   //open file and attempt to lock&lt;br /&gt;
    $fp = fopen($cfile , 'w');&lt;br /&gt;
    if( flock($fp, LOCK_EX)) {&lt;br /&gt;
      ob_start();&lt;br /&gt;
    } else {&lt;br /&gt;
   //if cant lock then return the previously generated cache&lt;br /&gt;
      include($cachefile);&lt;br /&gt;
     exit;&lt;br /&gt;
}&lt;br /&gt;
-------------------&lt;br /&gt;
&lt;br /&gt;
The worst problem here is fopen($cfile , 'w'); Opening with w instantly truncates the file, meaning that anything that falls through the flock will then blindly read an empty file. This is important because the exercise in this case was to make &quot;Better Php Caching&quot;.&lt;br /&gt;
&lt;br /&gt;
You could argue that there isn't much point to this if you don't have high traffic of several hits per second, but it's going to happen sooner or later.&lt;br /&gt;
&lt;br /&gt;
if(@readfile($cachefile)) exit(); is safer because if readfile returns zero bytes due to another script over-writing it, the page will still be generated instead of exiting.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by samee ( &lt;a href="http://www.ebncana.com"&gt;http://www.ebncana.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;I faced 2 questions :&lt;br /&gt;
&lt;br /&gt;
1- my webite code in one singel file ... it's work with cases so in the top of the file and before any case i send the header and the footer after the last break of last case  ... how can i make cache file for the header or the footer html file only and not for all php page ??&lt;br /&gt;
&lt;br /&gt;
2- In the ignore list when i set some dir as part of ignored array the code will not make cache file also for the files under this dir (f.e in the &quot; semo &quot; dir i like to make cache file for all pages under this dir except the file &quot;semo/index.php&quot; i dont like to make cache file for it ... how can i do that ... when i put the file &quot; semo/index.php&quot; on ignored list it's will prevent all the files on that dir form caching ... any solution ??&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
thank you all .</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by William Weijia Yang ( &lt;a href=""&gt;&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;I think the end_caching.php needs to be wrapped in a &quot;if&quot; or else it will always generate a cache file even if the page is in the ignore_list.  Should be something like:&lt;br /&gt;
&lt;br /&gt;
&lt;?php&lt;br /&gt;
 &lt;br /&gt;
if($ignore_page===false) {&lt;br /&gt;
    // Now the script has run, generate a new cache file&lt;br /&gt;
    $fp = @fopen($cachefile, 'w'); &lt;br /&gt;
 &lt;br /&gt;
    // save the contents of output buffer to the file&lt;br /&gt;
    @fwrite($fp, ob_get_contents());&lt;br /&gt;
    @fclose($fp); &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
    ob_end_flush(); &lt;br /&gt;
 &lt;br /&gt;
?&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by samee ( &lt;a href="http://www.ebncana.com"&gt;http://www.ebncana.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;well thank you soo much mr. Dave Child ...&lt;br /&gt;
&lt;br /&gt;
I see that there is lot of comments and this mean new updates for your script ... so what i wanna ask from you that if you please put all the updates in new code and put it in one zip file with examples so that every one can use your great cache system in his own script .&lt;br /&gt;
&lt;br /&gt;
thank you one more time and i hope u answer positively .</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by P ( &lt;a href="http://www.dezzignz.com/"&gt;http://www.dezzignz.com/&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;I read the comments above, and almost all of you talk about some issues related to your site's architecture.&lt;br /&gt;
&lt;br /&gt;
In my case, my site is run from a single &quot;file&quot; so I placed Dave's caching header and footer inside this &quot;file&quot;, in critically chosen spots. I didn't have to do anything with .htaccess. Everything worked very smoothly.&lt;br /&gt;
&lt;br /&gt;
Even method POST is not being handled by my &quot;file&quot;, but some other file instead.&lt;br /&gt;
&lt;br /&gt;
However, I am stuck with one -- can't seem to figure out why the caching doesn't work on my RSS feeds.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by Dave Child ( &lt;a href="http://www.addedbytes.com"&gt;http://www.addedbytes.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;@Dave: Quite right, POST should be ignored. In my version on this site, I had actually made that change, but hadn't updated this post. I'll review this post over the weekend and update it.&lt;br /&gt;
&lt;br /&gt;
@Nate: Your site is sending content before the session_start is called in index.php. I suggest you check for whitespace outsite of the &lt;?php ... ?&gt; tags in the begin_caching.php script.&lt;br /&gt;
&lt;br /&gt;
@dan: Thanks. Always nice to be called &quot;awesome&quot; :). I believe that if you clear the output cache, and then send a 404, you should avoid that problem. Not tested that though.&lt;br /&gt;
&lt;br /&gt;
@ojuicer: Good point. I've been thinking about this, and my feeling is it's unlilkely to be a major problem. Would you agree?&lt;br /&gt;
&lt;br /&gt;
@P: You're very welcome. Glad it's helped drop your loading times.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.addedbytes.com/article/caching-output-in-php/comments/</link><guid>http://www.addedbytes.com/article/caching-output-in-php/comments/</guid><description>Comment by P ( &lt;a href="http://www.dezzignz.com/"&gt;http://www.dezzignz.com/&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;In my case, running the dynamic file itself took about 220 msec.&lt;br /&gt;
&lt;br /&gt;
I implemented your caching and voila, I measured loading times as low as 0.2 msec and on average about 2 msec.&lt;br /&gt;
&lt;br /&gt;
Thanks for the tutorial!</description></item></channel></rss>