<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
  <channel>
    <title>luwes</title>
    <link>http://luwes.co/</link>
    <description>luwes is focused on web development, coding, interaction design</description>
    <language>en-us</language>           
    <generator>Nucleus CMS v3.64</generator>
    <copyright>ï¿½</copyright>             
    <category>Weblog</category>
    <docs>http://backend.userland.com/rss</docs>
    <image>
      <url>http://luwes.co//nucleus/nucleus2.gif</url>
      <title>luwes</title>
      <link>http://luwes.co/</link>
    </image>
    <item>
 <title>Time for a Change</title>
 <link>http://luwes.co/2012/01/time-for-a-change</link>
<description><![CDATA[<b>Happy belated New Year! I wish you all an exciting and successful 2012.</b><br />
<br />
As you can probably see, this site has moved from wessite.com to luwes.co and I thought a little message was in order to explain this change.<br />
<br />
It's quite simple, I didn't like the ring to <b>wessite</b> anymore and it's too similar to <b>website</b> as Google is always telling me :). After deciding the name had to change, I had in mind to use my real name wesleyluyten.com but it seemed a bit long and has some disadvantages.<br />
<br />
So after a bit of puzzling the name <b>luwes</b> popped out, it's just some parts of my real name stitched together... that make a reasonably sounding name.]]></description>
 <category>General</category>
<comments>http://luwes.co/2012/01/time-for-a-change</comments>
 <pubDate>Sun, 15 Jan 2012 14:01:49 +0100</pubDate>
</item><item>
 <title>AS3 Ray Box Intersection</title>
 <link>http://luwes.co/2010/12/as3-ray-box-intersection</link>
<description><![CDATA[When you are developing a game one thing is for certain, you will need math! Especially games with some sort of physics involved rely heavily on geometry calculations. I really regret that I didn't pay more attention in high school when we went over the basics of geometry, it would have been useful when I started building my game <a href="http://www.etchnride.com/">etchNride</a>.<br />
<br />
I needed the ray box intersection test for a "selection tool" I created for my 2D game. I thought it might be useful to someone so I decided to post it here.<br />
<br />
<div class="swfcontent"><div id="swf1"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash</a></div></div>
<script type="text/javascript">
var flashvars = {};
var params = {allowfullscreen:true, };
var attributes = {};
swfobject.embedSWF("http://luwes.co/media/intersection-ray-box.swf", "swf1", "520", "260", "10.0.0", false, flashvars, params, attributes);
</script><br />
There are two different functions included in the source that perform the actual intersection test. The first one is just a simple line segment - line segment intersection test which is executed 4 times, for each ray of the axis aligned rectangle box. The second function is based on "slabs" where a slab is the space between two parallel planes. I found the theory of this technique in the book "3D Computer Graphics" from Alan Watt and created a function from it. If performance is a must in your application you should implement the second function, it's slightly faster than the first.<br />
<br />
Download the source: <a href="http://luwes.co/?file=1/RayBoxIntersection.zip">RayBoxIntersection.zip</a><br />
Some alternative titles for this post:<br />
<ul><li>ray aabb intersection</li><li>ray axis aligned bounding box intersection</li></ul>]]></description>
 <category>Actionscript</category>
<comments>http://luwes.co/2010/12/as3-ray-box-intersection</comments>
 <pubDate>Fri, 24 Dec 2010 14:58:57 +0100</pubDate>
</item><item>
 <title>Spectrum Visualizer Version 1.2</title>
 <link>http://luwes.co/2010/10/spectrum-visualizer-version-12</link>
<description><![CDATA[Version 1.2 of the Spectrum Visualizer is released. This update brings a few cool new features: set a background grid in a color of your choice, show peaks for a configurable amount of time, set a spacing between left and right channel and a few more. All flashvars are documented in the labs, <a href="/labs/spectrumvisualizer">Spectrum Visualizer</a><br />
<br />
<div class="swfcontent"><div id="swf2"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash</a></div></div>
<script type="text/javascript">
var flashvars = {screencolor:"eeeeee", backcolor:"dddddd", duration:227, frontcolor:"009900", file:"/media/ready-or-not.m4a", plugins:"http://luwes.co/skins2.3.5/spectrumvisualizer-1.swf", "spectrumvisualizer.gridcolor":"ffdddddd", "spectrumvisualizer.colortype":"solid", "spectrumvisualizer.color":"ff009900", "spectrumvisualizer.numbars":64, "spectrumvisualizer.hidepause":false, "spectrumvisualizer.xpos":20, "spectrumvisualizer.ypos":12, "spectrumvisualizer.stepdown":3, "spectrumvisualizer.xlen":441, "spectrumvisualizer.ylen":56, "spectrumvisualizer.hspacing":17, controlbar:"left", "controlbar.size":79, skin:"http://luwes.co/skins2.3.5/soundwave-skin.swf"};
var params = {allowfullscreen:true, };
var attributes = {};
swfobject.embedSWF("http://luwes.co/skins2.3.5/player.swf", "swf2", "520", "79", "10.0.0", false, flashvars, params, attributes);
</script>]]></description>
 <category>Actionscript</category>
<comments>http://luwes.co/2010/10/spectrum-visualizer-version-12</comments>
 <pubDate>Thu, 21 Oct 2010 16:24:36 +0200</pubDate>
</item><item>
 <title>App Engine - Updating Your Model&apos;s Schema</title>
 <link>http://luwes.co/2010/10/app-engine-updating-your-model-s-schema</link>
<description><![CDATA[I have been doing a lot of coding for my web game, <a href="http://www.etchnride.com/">www.etchnride.com</a>, I released last month. I chose to host it on App Engine for its ability to scale well and it's been a real pleasure developing on it so far.<br />
<br />
Naively I designed a "User" model for my application to store the entities of the game members. The model used the username as a key, and all other models associated with that member would contain a "StringProperty" with the username. Later on I discovered that this isn't the preferred way of making entity relationships because you're sacrificing flexibility. For example, if you would give the members the possibility to change their username, the script would have to change all the entities associated with that member and would need to handle the issue with the unchangeable key of the "User" entity.<br />
<br />
<b>A better design is to use a number as the key and create relationships with the "ReferenceProperty", that way if a username needs to change there's only one entity to update.</b><br />
<br />
Back to the point of this article, if your app is already launched and holds a lot of entities you can't just start from scratch again to make a new design for your model, you will have to <i>update your model's schema</i>.<br />
<br />
Google has a great article regarding this issue, <a href="http://code.google.com/appengine/articles/update_schema.html">Updating Your Model's Schema</a>. The code snippet that is shown there requires that you have a property with a unique value, which is not always the case. I wrote a more generalized updating script that uses the entity key to order on, that way you don't necessarily need a property with a unique value. <br />
<br />
<div class="python"><span style="color: #808080; font-style: italic;"># Request handler for the URL /update_datastore</span><br />
<span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; keystr = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'key'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; model = <span style="color: #008000;">self</span>.<span style="color: black;">request</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'model'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> model:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> keystr <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># First request, just get the first name out of the datastore.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj = db.<span style="color: black;">GqlQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SELECT * FROM '</span> + model + <span style="color: #483d8b;">' ORDER BY __key__ DESC'</span><span style="color: black;">&#41;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> obj:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keystr = obj.<span style="color: black;">key</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> keystr:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key = Key<span style="color: black;">&#40;</span>keystr<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q = db.<span style="color: black;">GqlQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SELECT * FROM '</span> + model + <span style="color: #483d8b;">' WHERE __key__ &lt;= :1 ORDER BY __key__ DESC'</span>, key<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objects = q.<span style="color: black;">fetch</span><span style="color: black;">&#40;</span>limit=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> objects:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_obj = objects<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#write your update logic here</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_obj.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>objects<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">2</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_name = objects<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">key</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_url = <span style="color: #483d8b;">'/update_datastore?model='</span> + model + <span style="color: #483d8b;">'&amp;key='</span> + <span style="color: #dc143c;">urllib</span>.<span style="color: black;">quote</span><span style="color: black;">&#40;</span>next_name<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_name = <span style="color: #483d8b;">'FINISHED'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_url = <span style="color: #483d8b;">'/'</span> &nbsp;<span style="color: #808080; font-style: italic;"># Finished processing, go back to main page.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context = <span style="color: black;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'current_name'</span>: keystr,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'next_name'</span>: next_name,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'next_url'</span>: next_url,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'templates/update_datastore.html'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">response</span>.<span style="color: black;">out</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>template.<span style="color: black;">render</span><span style="color: black;">&#40;</span>path, context<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div><br />
HTML template after the break.<br />
<br />
<div class="html4strict"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">meta</span></a> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;refresh&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1;url={{ next_url }}&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h3.html"><span style="color: #000000; font-weight: bold;">h3</span></a>&gt;</span>Update Datastore<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h3.html"><span style="color: #000000; font-weight: bold;">h3</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>Updated: {{ current_name }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span>About to update: {{ next_name }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div>]]></description>
 <category>App Engine</category>
<comments>http://luwes.co/2010/10/app-engine-updating-your-model-s-schema</comments>
 <pubDate>Thu, 21 Oct 2010 11:53:47 +0200</pubDate>
</item><item>
 <title>Spectrum Visualizer Plugin - JW Player</title>
 <link>http://luwes.co/2009/10/spectrum-visualizer-plugin-jw-player</link>
<description><![CDATA[A few months ago I started out with Actionscript 3, the programming language to develop on the Adobe Flash Player platform. At the time I wanted to get a “bar spectrum visualization” to show in the browser, like on a real stereo system. I found out Adobe had introduced a new class called SoundMixer in the Flash Player 9 release. This was just what I needed as it gives you the possibility to retrieve the frequencies of music playing in the Flash Player.<br />
<br />
I already had a great music player, the JW player from Longtail Video, so the obvious move was to create a plugin and get the sound visualization on the screen of the JW player. Looking on the internet for similar projects I stumbled upon the blog of <a href="http://www.everydayflash.com/">Bartek Drozdz</a>, he had made a equalizer class in AS3 and posted the source code on his website free to use. This is the result I ended up with. Everybody can get it free of charge at <a href="http://www.longtailvideo.com/addons/plugins/79/Spectrum-Visualizer">Longtail Video's addons section</a>.<br />
<br />
<div class="swfcontent"><div id="swf3"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash</a></div></div>
<script type="text/javascript">
var flashvars = {file:"/media/ready-or-not.m4a", backcolor:"000000", frontcolor:"ffffff", screencolor:"000000", plugins:"http://luwes.co/skins2.3.5/spectrumvisualizer-1.swf", "spectrumvisualizer.effect":"reflection", "spectrumvisualizer.xpos":5, "spectrumvisualizer.stepdown":5};
var params = {allowfullscreen:true, };
var attributes = {};
swfobject.embedSWF("http://luwes.co/skins2.3.5/player.swf", "swf3", "520", "200", "10.0.0", false, flashvars, params, attributes);
</script><br />
A demo page with different configurations can be found in the Labs: <a href="/labs/spectrumvisualizer">Compute Spectrum Visualizer</a>]]></description>
 <category>Actionscript</category>
<comments>http://luwes.co/2009/10/spectrum-visualizer-plugin-jw-player</comments>
 <pubDate>Sun, 11 Oct 2009 21:21:59 +0200</pubDate>
</item><item>
 <title>Add Far Future Expires Headers</title>
 <link>http://luwes.co/2008/11/add-far-future-expires-headers</link>
<description><![CDATA[This is the third part of the article <a href="/2008/11/speeding-up-your-nucleus-cms-web-site">Speeding Up your Web Site</a>. The method is really easy to configure and will drastically increase the speed of your web page. I found this optimizing tip at <a href="http://www.askapache.com/htaccess/apache-speed-expires.html">askapache.com</a>:<br />
<br />
<div class="quote">A first-time visitor to your page will make several HTTP requests to download all your sites files, but using the Expires header you make those files cacheable. This avoids unnecessary HTTP requests on subsequent page views.</div><br />
Just put the following lines in your <code>.htaccess</code> file:<br />
<br />
<div class="apache">&lt;<span style="color: #000000; font-weight:bold;">FilesMatch</span> <span style="color: #7f007f;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$&quot;</span>&gt;<br />
<span style="color: #00007f;">Header</span> set Expires <span style="color: #7f007f;">&quot;Thu, 15 Apr 2010 20:00:00 GMT&quot;</span><br />
&lt;/<span style="color: #000000; font-weight:bold;">FilesMatch</span>&gt;</div><br />
Store the <code>.htaccess</code> file in the directory from where you want to start adding the far future header, so if you store this in the root all the files on your server with the extensions above will get a far future expires header.<br />
<div class="note"><b>Note:</b> if you use a <b>far future Expires header</b> you have to change the component’s file name whenever the file changes.</div><br />
<div class="leftbox"><a href="http://luwes.co/media/1/20081123-wessite_speed3.jpg" onclick="Foto(this.href, 'Speed Graph with Far Expires Header', '800', '469'); return false;"><img src="http://luwes.co/media/1/thumb_20081123-wessite_speed3.jpg" width="229" height="134" alt="Speed Graph with Far Expires Header" /></a></div>By adding these headers I've reduced the load time of this website for the second page view from 3.55 sec to 1.74 seconds. Actually the page rendered completely in 1.16 seconds, but the loading of the video playlists which is asynchronous added some to the total time. At the left you can see a speed graph created by Safari.<br />
<br />
For Nucleus users I also have an additional tip to make maintaining your website even easier. Check it out below.<br />
<br />
As noted above you should always change the component’s file name whenever you change a file with a far future expires header. This can be a real drag if you change your files a lot. A workaround is instead of changing the filename, just change the name of your skin folder at the "configuration" page in the admin area.<br />
<br />
For example to: http://your-nucleus-website.com/skins1.0.1/<br />
<br />
Then with a bit of mod_rewrite magic we can point to the original skin folder at:<br />
<br />
For example: http://your-nucleus-website.com/skins/<br />
<br />
...add this line to the <code>.htaccess</code> file in your root replacing the "default" with your current skin name<br />
<br />
<div class="apache"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
<span style="color: #00007f;">RewriteBase</span> /<br />
<span style="color: #00007f;">RewriteRule</span> ^skins.*/default/(.*)$ skins/default/$1</div><br />
<div class="note"><b>Note:</b> if your preexisting .htaccess file already uses URL rewriting you do not need to add the first two lines. You can simply add the last line to the bottom of the .htaccess file</div><br />
Now you just have to change 1 setting at the configuration page instead of changing al the filenames of the files you edited.<br />
]]></description>
 <category>Optimization</category>
<comments>http://luwes.co/2008/11/add-far-future-expires-headers</comments>
 <pubDate>Sun, 23 Nov 2008 12:32:58 +0100</pubDate>
</item><item>
 <title>Combining and Compressing Javascript and CSS Files</title>
 <link>http://luwes.co/2008/11/combining-and-compressing-javascript-and-css-files</link>
<description><![CDATA[<b>Reducing the number of HTTP requests is the key to faster pages.</b> As read in the best practices for speeding up your web site:<br />
<br />
<div class="quote">80% of the end-user response time is spent on the front-end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages. Combined files are a way to reduce the number of HTTP requests by combining all scripts into a single script, and similarly combining all CSS into a single stylesheet.</div><br />
<a href="http://alistapart.com/articles/sprites">CSS Sprites</a> is another way to reduce the number of requests but we wont be handling sprites in this article.<br />
<br />
So combining of the javascript and CSS files, you can do this either by manually putting all the javascript files in one large file or let a script handle the combining for you. I think I'm gonna choose the latter. The combining script I use on this site is created by a dutch programmer called Rakaz and can be found at this link: <a href="http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files">Make your pages load faster by combining and compressing javascript and css files</a><br />
<br />
This PHP script is awesome, it not only combines your files but also compresses it and stores a cache for instant response time. So just follow the guidelines at the page above and you'll be speeding up your web site in no time. For this site it made a noticeable difference and cut down the load time from 4.69 sec to 3.55 sec.<br />
<br />
For Nucleus CMS users I have tweaked the script a little for ultimate usability, read on below.Download the customized script for Nucleus users at this link: <a href="http://luwes.co/?file=1/combine.php.zip">Combining JS and CSS Script</a><br />
<br />
<b>How to use this script?</b><br />
<br />
<u>Step 1</u> Download the script above, open it and at the top change the variable <span class="php"><span style="color: #000088;">$cachedir</span></span> to the directory where you are gonna cache your combined files, if you don't have such a directory create one and make sure it is writable. Secondly change the <span class="php"><span style="color: #000088;">$dir</span></span> variable to the directory of your skin. You should get something like:<br />
<br />
<div class="php"><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$cachedir</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/cache'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/dirname"><span style="color: #990000;">dirname</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/skins/default'</span><span style="color: #339933;">;</span></div><br />
Upload the combine.php script to the root of your website.<br />
<br />
<u>Step 2</u> You need to create or modify your .htaccess file. If you do not have a .htaccess file you can create it in the root of your website and add the following lines. If you already have an preexisting .htaccess file you can simply add the following lines to the file where you have to replace <span class="apache">default</span> with your current skin name:<br />
<br />
<div class="apache"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
<span style="color: #00007f;">RewriteBase</span> /<br />
<span style="color: #00007f;">RewriteRule</span> ^skins.*/default/([^/]+\.css|[^/]+\.js)$ combine.php?files=$1</div><br />
<div class="note"><b>Note:</b> if your preexisting .htaccess file already uses URL rewriting you do not need to add the first two lines. You can simply add the last line to the bottom of the .htaccess file</div><br />
<u>Step 3</u> Change the js and css lines in your head.inc file in the admin area at "skin files" area to something like:<br />
<br />
<div class="html4strict"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;%skinfile(newstyle.css|nicetitle.css)%&gt;</span></span>&quot; /&gt;<br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;%skinfile(nicetitle.js|swfobject.js|popup.js)%&gt;</span></span>&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div><br />
]]></description>
 <category>Optimization</category>
<comments>http://luwes.co/2008/11/combining-and-compressing-javascript-and-css-files</comments>
 <pubDate>Sun, 23 Nov 2008 11:15:22 +0100</pubDate>
</item><item>
 <title>Caching Solutions for Nucleus CMS</title>
 <link>http://luwes.co/2008/11/caching-solutions-for-nucleus-cms</link>
<description><![CDATA[I have been using the Nucleus content management system for a few years, and I really like it. I once installed the Drupal on my Dreamhost server, and it was really slow compared to nucleus. The nucleus core is lightweight which makes it pretty fast, but not fast enough for my sluggish server though. It still makes a lot of SQL queries and has to read and parse templates which takes a lot of time. So how do we solve this problem you ask? One solution is; by caching your web page server side.<br />
<br />
There are 2 plugins available for caching that I know of:<br />
<br />
<ul><li><a href="http://wakka.xiffy.nl/cache">NP_Cache</a></li><li><a href="http://forum.nucleuscms.org/viewtopic.php?t=16040">NP_CacheLite</a></li></ul><br />
I never used the NP_CacheLite, but I took a look at the code and I didn't really like it. This plugin gives you the possibility of putting dynamic content between no cache tags in your skin, which means it still has to fetch your skin from your database. Maybe it's good for some websites, but not for this one.<br />
<br />
I went for the NP_Cache plugin, a fully cached solution for your website. Using a full cache of your web page is great but it comes with some glitches. Everything is static, this means the cache file has to be deleted every time a visitor comments on a item, or gives a rating, or when a user logs in for example. So everything which used to be dynamic has ceased to work. A few examples of plugins which won't work are NP_Captcha, NP_Views, NP_ Refer. You can address this issue by getting some dynamic content through an AJAX request.Below is my customized code for NP_Cache:<br />
<br />
<div class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> NP_Cache <span style="color: #000000; font-weight: bold;">extends</span> NucleusPlugin <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$cache_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getMinNucleusVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">155</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Nucleus Cache'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getAuthor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Jim Burnett | Wesley Luyten'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'http://www.mediauncut.com/'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'1.1.01'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Enables caching of nucleus items, homepage, searches and anything off index.php.'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getEventList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PreSkinParse'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'PostSkinParse'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'PostAddItem'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'PreUpdateItem'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> install<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_dir'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Enter the path (no trailing slash) of the cache directory: '</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/tmp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_dir</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">obStarted</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> event_PreSkinParse<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/ob_start"><span style="color: #990000;">ob_start</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$bStart</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// don't start output buffering if we're not at the top level</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// (fixes problems with RSS feeds &amp; ETAG)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/function_exists"><span style="color: #990000;">function_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ob_get_level'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/ob_get_level"><span style="color: #990000;">ob_get_level</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$bStart</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bStart</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">obStarted</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ob_start"><span style="color: #990000;">ob_start</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ob_gzhandler&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> event_PostAddItem<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$blogid</span> <span style="color: #339933;">=</span> getBlogIDFromItemID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'itemid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> createItemLink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'itemid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteCacheFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> createBlogidLink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blogid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteCacheFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> event_PreUpdateItem<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$blogid</span> <span style="color: #339933;">=</span> getBlogIDFromItemID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'itemid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> createItemLink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'itemid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteCacheFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> createBlogidLink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$blogid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteCacheFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> deleteCacheFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">convertURL</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cache_file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/cache-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cache_file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/cache-'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> event_PostSkinParse<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/getenv"><span style="color: #990000;">getenv</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">convertURL</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$cache_file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_dir</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/cache-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// if these string values occur in the url don't cache the page</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$dont_cache</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'query'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'catid'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'archive'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'gif'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'png'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'jpg'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'member'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/str_ireplace"><span style="color: #990000;">str_ireplace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dont_cache</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/fopen"><span style="color: #990000;">fopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cannot open file (<span style="color: #006699; font-weight: bold;">$cache_file</span>)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/fwrite"><span style="color: #990000;">fwrite</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <a href="http://www.php.net/ob_get_contents"><span style="color: #990000;">ob_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Cannot write to file (<span style="color: #006699; font-weight: bold;">$cache_file</span>)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/ob_end_flush"><span style="color: #990000;">ob_end_flush</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> convertURL<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$CONF</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$CONF</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'IndexURL'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$replace</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div><br />
Notice the <span class="php"><span style="color: #000088;">$dont_cache</span></span> array. If a string value of the array occurs in the url of the web page the page won't be cached. You should alter this array to whatever fits you.<br />
<br />
You can download the customized plugin here: <a href="http://luwes.co/?file=1/NP_Cache.php.zip">NP_Cache</a><br />
<br />
<b>How to install and use the plugin?</b><br />
Download the plugin above and save it to a file called NP_Cache.php in your nucleus/plugins directory.<br />
<br />
<u>Step 1</u> Install NP_Cache.php from the Plugin Management page of the Nucleus admin area. After installing, edit the options for the NP_Cache plugin from that same page. Set the cache dir to match the absolute directory on your server. This directory must exist and must be writable by the web server. For Example: /home/myname/www.mysite.com/cache<br />
<br />
<u>Step 2</u> This is the code you want to add to the beginning of your root index.php and for each blog. Replace the “/home/myname/www.mysite.com/cache” with the same directory path you set in the plugin options, be sure to surround it in quotes. Add it directly after the &lt;?php line<br />
<br />
<div class="php"><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/home/myname/www.mysite.com/cache/cache-'</span><span style="color: #339933;">.</span><a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><a href="http://www.php.net/getenv"><span style="color: #990000;">getenv</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// cache files will be deleted every 1 day = 24*60*60</span><br />
<br />
<span style="color: #666666; font-style: italic;">// delete cache file when someone comments</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'addcomment'</span><span style="color: #009900;">&#41;</span> <a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color: #990000;">file_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$t</span> <span style="color: #339933;">&lt;</span> <a href="http://www.php.net/filemtime"><span style="color: #990000;">filemtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/file_get_contents"><span style="color: #990000;">file_get_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;!-- From cache generated '</span><span style="color: #339933;">.</span><a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'H:i'</span><span style="color: #339933;">,</span><a href="http://www.php.net/filemtime"><span style="color: #990000;">filemtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' by NP_Cache --&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div><br />
<div class="leftbox"><a href="http://luwes.co/media/1/20081122-wessite_speed1.jpg" onclick="Foto(this.href, 'Speed Graph with NP_Cache plugin', '800', '417'); return false;"><img src="http://luwes.co/media/1/thumb_20081122-wessite_speed1.jpg" width="229" height="119" alt="Speed Graph with NP_Cache plugin" /></a></div>That's it, if everything went ok you will have a noticeable faster website. On the left is a speed graph of this website measured by Safari. The load time went from approximately 8.28 sec to 4.69 seconds with NP_Cache.]]></description>
 <category>Nucleus</category>
<comments>http://luwes.co/2008/11/caching-solutions-for-nucleus-cms</comments>
 <pubDate>Sat, 22 Nov 2008 19:59:15 +0100</pubDate>
</item><item>
 <title>Speeding Up your (Nucleus CMS) Web Site</title>
 <link>http://luwes.co/2008/11/speeding-up-your-nucleus-cms-web-site</link>
<description><![CDATA[I have been wanting to write this article for quite a time now, and today I found the time to do it. First off I want to say I'm not an expert or something when it comes to optimizing websites but I try to do my best, and share my thoughts and findings with you guys. This article will explain how I reduced the load time of this nucleus website from approximately  8.28 sec to 1.74 sec. Why is this important, well because:<br />
<br />
<div class="quote">Recent research shows that website users will not wait longer than 4 seconds for a website to load. After 4 seconds, customers will leave the site and seek an alternative site. Alarmingly, the research also shows that if a site fails to load within 4 seconds, users will be left with a negative brand perception.</div><br />
This website is served by the well known hosting service <a href="http://www.dreamhost.com/r.cgi?297983">DreamHost</a>. I have been very pleased with their services so far, they provide plenty web space and bandwidth for a blog (for sure) and their customer support is also awesome. The only lack is their servers aren't the fastest around especially when you make a lot of http requests and SQL queries, but hey what can you ask for such a low price.<br />
<div class="leftbox"><a href="http://luwes.co/media/1/20081122-wessite_speed0.jpg" onclick="Foto(this.href, 'Speed Graph without Optimization', '800', '417'); return false;"><img src="http://luwes.co/media/1/thumb_20081122-wessite_speed0.jpg" width="229" height="119" alt="Speed Graph without Optimization" /></a></div>I thought it would be better to split this article in a few parts because some of the methods that will be used are only for <a href="http://www.nucleuscms.org/">nucleus CMS</a> users and others are for all web designers. On the left you will see an image of the load time of my website without using any of the optimizing methods, it fully loaded in a time of 8.28 seconds (measured with Safari).<br />
<br />
<br />
<br />
The following links are the parts of the complete article:<br />
<br />
<ul><li><a href="/2008/11/caching-solutions-for-nucleus-cms">Caching Solutions for Nucleus CMS</a></li><li><a href="/2008/11/combining-and-compressing-javascript-and-css-files">Combining and Compressing Javascript and CSS Files</a></li><li><a href="/2008/11/add-far-future-expires-headers">Add Far Future Expires Headers</a></li></ul><br />
Resources you should explore if you are serious about optimizing your website:<br />
<br />
<ul><li><a href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a></li><li><a href="http://www.askapache.com/">AskApache Web Development</a></li></ul>]]></description>
 <category>Optimization</category>
<comments>http://luwes.co/2008/11/speeding-up-your-nucleus-cms-web-site</comments>
 <pubDate>Sat, 22 Nov 2008 17:59:00 +0100</pubDate>
</item><item>
 <title>Digg Like Time Ago PHP Function</title>
 <link>http://luwes.co/2008/11/digg-like-time-ago-php-function</link>
<description><![CDATA[A few days ago I was looking for a php function that showed the date/time format like on the site Digg (ex: 15 min 20 sec ago). I found two different php functions on the internet that almost achieved what I was looking for, at these links:<br />
<br />
<a href="http://woork.blogspot.com/2007/10/time-and-date-difference-using-php.html">woork.blogspot.com/2007/10/time-and-date-difference-using-php.html</a><br />
<a href="http://drupal.org/node/61565">drupal.org/node/61565</a><br />
<br />
But it wasn't exactly what I wanted so I wrote my own php function for it, check it out  below.<br />
<br />
<div class="php"><span style="color: #000000; font-weight: bold;">function</span> timeAgo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$granularity</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$format</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$difference</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timestamp</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'0 seconds ago'</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// if difference is lower than zero check server offset</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">864000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// if difference is over 10 days show normal time form</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$periods</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'week'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">604800</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'day'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'hr'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'sec'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$periods</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$difference</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$difference</span> <span style="color: #339933;">%=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span> ? <span style="color: #0000ff;">' '</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$time</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'day'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'s'</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$granularity</span><span style="color: #339933;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$granularity</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span> ? <span style="color: #000088;">$output</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'0 seconds'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' ago'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$format</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div>The first argument in the function is just a normal time stamp of your item which you probably fetch from your MySQL, the second "granularity" is how far you go in the precision of your date. For example:<br />
<br />
-granularity = 4 => 1day, 5hr, 23min, 12sec ago<br />
-granularity = 2 => 1day, 5hr ago<br />
<br />
The third parameter is the date format to be used when the time difference is over 10 days (notice the 864000 in the function, that's 10*24*60*60), so when the difference is 10 days or over the function shows the date in the format you choose. Of course you can change the 10 days in to whatever you like.<br />
<br />
You can download the php file here: <a href="http://luwes.co/?file=1/timeAgo.php.zip">PHP Time Ago Function</a><br />
<br />
<b>How to use it?</b><br />
<br />
Just include the timeAgo.php file in your PHP file that will use the function.<br />
<br />
<div class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timeAgo.php'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div><br />
...and call the function when needed:<br />
<br />
<div class="php">timeAgo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dateRef</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div><br />
...where $dateRef is, for example, a value from a SQL query.<br />
<br />
]]></description>
 <category>PHP/MySQL</category>
<comments>http://luwes.co/2008/11/digg-like-time-ago-php-function</comments>
 <pubDate>Wed, 12 Nov 2008 15:07:42 +0100</pubDate>
</item>
  </channel>
</rss>
