<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to build a simple caching system, with PHP</title>
	<link>http://phpit.net/article/build-caching-system-php/</link>
	<description>PHPit has dozens of PHP articles, codesnippets and FAQ's.</description>
	<pubDate>Fri, 13 Jun 2008 02:23:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>

	<item>
		<title>by: keitai</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-869</link>
		<pubDate>Wed, 22 Mar 2006 11:38:03 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-869</guid>
					<description>hi there,

Trying to follow and implement your caching functions, but

One question how about $content how is this one set??</description>
		<content:encoded><![CDATA[<p>hi there,</p>
<p>Trying to follow and implement your caching functions, but</p>
<p>One question how about $content how is this one set??
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Johanna Nordin</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-621</link>
		<pubDate>Sun, 19 Feb 2006 03:13:31 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-621</guid>
					<description>&lt;strong&gt; Santana Juelz What The Game's Been Missing&lt;/strong&gt;

PHPit - Totally PHP &#38;r...</description>
		<content:encoded><![CDATA[<p><strong> Santana Juelz What The Game&#8217;s Been Missing<br />
</strong></p>
<p>PHPit - Totally PHP &amp;r&#8230;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Dennis Pallett</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-362</link>
		<pubDate>Thu, 05 Jan 2006 18:17:18 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-362</guid>
					<description>Guess it ain't working. Let me know what you want to post at dennis [AT] phpit [DOT] net, and I'll add it to the article it self.</description>
		<content:encoded><![CDATA[<p>Guess it ain&#8217;t working. Let me know what you want to post at dennis [AT] phpit [DOT] net, and I&#8217;ll add it to the article it self.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Hutch</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-358</link>
		<pubDate>Thu, 05 Jan 2006 17:26:20 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-358</guid>
					<description>The cache system can be modified to work with personal/secure pages as such:

For regular non user/secure pages:
Change the cached pages to .php not .html.
Change the data for the cache file to $data = '&#60;?php echo&#34; '.addslashes($content).' &#34;;?&#62;';


When getting the cached pages use mktime (as previously suggested) and simply include the file ie:
// Cache too old
if (mktime()-(cache_time*60*60) &#62; filemtime($file)){
	     return false;
}
include($file);
die();

NOTE: I haven't done this as my site is trivial but I believe it should work
For personalized pages I suggest a shorter cache time so define a second variable secure_cache_time.  At the top of any pages that are secure create a flag to identify as such define('secure','1') to the "cache engine".
For the filename use a combination of the url and a unique user identifier (username, userid, ip) or some combination.
For the data $data = '&#60;?php if(!correct_user) die; else echo&#34; '.addslashes($content).' &#34;;?&#62;';
IMPORTANT: where correct_user is the logic you would regularly check for user credentials.  Finally for added security setup an htaccess file on the directory or put the user cached pages outside the root directory all together.

Hacking in a method to define a per page cache_time would be a good idea since we generally know how often certain pages update and I also added a refresh_cache function.  At the moment it can't determine which files it needs updating so it just removes all of them (more of a refresh_all function).</description>
		<content:encoded><![CDATA[<p>The cache system can be modified to work with personal/secure pages as such:</p>
<p>For regular non user/secure pages:<br />
Change the cached pages to .php not .html.<br />
Change the data for the cache file to $data = &#8216;&lt;?php echo&quot; &#8216;.addslashes($content).&#8217; &quot;;?&gt;&#8217;;</p>
<p>When getting the cached pages use mktime (as previously suggested) and simply include the file ie:<br />
// Cache too old<br />
if (mktime()-(cache_time*60*60) &gt; filemtime($file)){<br />
	     return false;<br />
}<br />
include($file);<br />
die();</p>
<p>NOTE: I haven&#8217;t done this as my site is trivial but I believe it should work<br />
For personalized pages I suggest a shorter cache time so define a second variable secure_cache_time.  At the top of any pages that are secure create a flag to identify as such define(&#8217;secure&#8217;,'1&#8242;) to the &#8220;cache engine&#8221;.<br />
For the filename use a combination of the url and a unique user identifier (username, userid, ip) or some combination.<br />
For the data $data = &#8216;&lt;?php if(!correct_user) die; else echo&quot; &#8216;.addslashes($content).&#8217; &quot;;?&gt;&#8217;;<br />
IMPORTANT: where correct_user is the logic you would regularly check for user credentials.  Finally for added security setup an htaccess file on the directory or put the user cached pages outside the root directory all together.</p>
<p>Hacking in a method to define a per page cache_time would be a good idea since we generally know how often certain pages update and I also added a refresh_cache function.  At the moment it can&#8217;t determine which files it needs updating so it just removes all of them (more of a refresh_all function).
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Laszlo Baranyai</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-96</link>
		<pubDate>Thu, 10 Nov 2005 06:47:58 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-96</guid>
					<description>File lock could improve this cache system. What will happen if concurrent visitors are reading the page? The first will truncate the cache file to zero length and starts writing; the second could receive partial content or corrupt the cache data by writing simultaneously. I recently ran into a long discussion about this situation on a mailing list. :)
Thanks for the article.
Best regards, Laszlo.</description>
		<content:encoded><![CDATA[<p>File lock could improve this cache system. What will happen if concurrent visitors are reading the page? The first will truncate the cache file to zero length and starts writing; the second could receive partial content or corrupt the cache data by writing simultaneously. I recently ran into a long discussion about this situation on a mailing list. :)<br />
Thanks for the article.<br />
Best regards, Laszlo.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Dennis Pallett</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-90</link>
		<pubDate>Wed, 09 Nov 2005 12:25:06 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-90</guid>
					<description>I fixed the get_url() logic, it was a small mistake that comes from my testing.

Does it really matter if you use time() instead of mktime()? Both return the same thing anyway.

I like your idea of using filemtime instead of using my method. It's probably a bit faster, and easier.

I understand that chmod'ing to 777 is a bad thing, but at least it works in all cases. Better of course would be something like 776 or 664.

Thanks for your comments!</description>
		<content:encoded><![CDATA[<p>I fixed the get_url() logic, it was a small mistake that comes from my testing.</p>
<p>Does it really matter if you use time() instead of mktime()? Both return the same thing anyway.</p>
<p>I like your idea of using filemtime instead of using my method. It&#8217;s probably a bit faster, and easier.</p>
<p>I understand that chmod&#8217;ing to 777 is a bad thing, but at least it works in all cases. Better of course would be something like 776 or 664.</p>
<p>Thanks for your comments!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Duncan Crombie</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-89</link>
		<pubDate>Wed, 09 Nov 2005 10:57:48 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-89</guid>
					<description>Forgot the most imporant:

- NEVER chmod to 777!!!</description>
		<content:encoded><![CDATA[<p>Forgot the most imporant:</p>
<p>- NEVER chmod to 777!!!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Duncan Crombie</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-88</link>
		<pubDate>Wed, 09 Nov 2005 10:56:22 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-88</guid>
					<description>I haven't actually run the script, but can see some problems with this script:

- use time() instead of mktime() to get the current timestamp
- the logic in get_url() seems to be reversed: if (!isset($_SERVER['REQUEST_URI'])) $url = $_SERVER['REQUEST_URI'];
- why load the entire cache file into memory just to check the date?  I would use filemtime instead...

;)</description>
		<content:encoded><![CDATA[<p>I haven&#8217;t actually run the script, but can see some problems with this script:</p>
<p>- use time() instead of mktime() to get the current timestamp<br />
- the logic in get_url() seems to be reversed: if (!isset($_SERVER[&#8217;REQUEST_URI&#8217;])) $url = $_SERVER[&#8217;REQUEST_URI&#8217;];<br />
- why load the entire cache file into memory just to check the date?  I would use filemtime instead&#8230;</p>
<p>;)
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ramy</title>
		<link>http://phpit.net/article/build-caching-system-php/#comment-78</link>
		<pubDate>Wed, 09 Nov 2005 00:35:04 +0000</pubDate>
		<guid>http://phpit.net/article/build-caching-system-php/#comment-78</guid>
					<description>Dear Sir/Madam,

this is really good but I am having problem accesing to the next page.
Thank you.

Regards,

Ramy Tran.</description>
		<content:encoded><![CDATA[<p>Dear Sir/Madam,</p>
<p>this is really good but I am having problem accesing to the next page.<br />
Thank you.</p>
<p>Regards,</p>
<p>Ramy Tran.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
