<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>phoenixheart - portfolio &#38; more &#187; Featured</title> <atom:link href="http://www.phoenixheart.net/category/featured/feed/" rel="self" type="application/rss+xml" /><link>http://www.phoenixheart.net</link> <description>phoenixheart - portfolio &#38; more</description> <lastBuildDate>Wed, 25 Aug 2010 03:02:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <script type="text/javascript">/*<![CDATA[*/if(typeof Meebo=="undefined"){Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)};(function(q){var args=arguments;if(!document.body){return setTimeout(function(){args.callee.apply(this,args)},100);}var d=document,b=d.body,m=b.insertBefore(d.createElement('div'),b.firstChild);s=d.createElement('script');m.id='meebo';m.style.display='none';m.innerHTML='<iframe id="meebo-iframe"></iframe>';s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network;b.insertBefore(s,b.firstChild);})({network:'phoenixheartnet_bo16we'});}/*]]>*/</script> <item><title>How to configure nginx to run Kohana on Ubuntu</title><link>http://www.phoenixheart.net/2010/03/how-to-configure-nginx-to-run-kohana-on-ubuntu/</link> <comments>http://www.phoenixheart.net/2010/03/how-to-configure-nginx-to-run-kohana-on-ubuntu/#comments</comments> <pubDate>Mon, 01 Mar 2010 07:20:23 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[configure]]></category> <category><![CDATA[kohana]]></category> <category><![CDATA[nginx]]></category> <category><![CDATA[virtual host]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=440</guid> <description><![CDATA[As a web developer I&#8217;ve been using Apache for a long long time. Recently though, I&#8217;ve started to move away from Apache in favor of nginx (pronounced &#8220;engine-X&#8221;). It&#8217;s not that I really need its strength, it&#8217;s just that I wanted to learn something new to break my box. It&#8217;s fairly simple to set up [...]]]></description> <content:encoded><![CDATA[<p>As a web developer I&#8217;ve been using Apache for a long long time. Recently though, I&#8217;ve started to move away from Apache in favor of <a
title="nginx's homepage" href="http://nginx.org/">nginx</a> (pronounced &#8220;engine-X&#8221;). It&#8217;s not that I really need <a
title="nginx compared to Apache" href="http://www.joeandmotorboat.com/2008/02/28/apache-vs-nginx-web-server-performance-deathmatch/">its strength</a>, it&#8217;s just that I wanted to learn something new to break my box.</p><p>It&#8217;s fairly simple to set up and get nginx running with FastCGI and MySQL on Ubuntu &#8211; a very well-written tutorial can be read <a
href="http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-ubuntu-8.10">on HowtoForge</a>, which should take you less than 15 minutes for everything. In this article therefore I will only write about how to configure nginx to actually run a <a
href="http://www.kohanaphp.com/">Kohana</a>-powered site, with virtual host and rewriting and such. If you&#8217;re not familiar with Kohana, take a look at <a
href="http://www.phoenixheart.net/2009/01/kohana-php-framework/">my article here</a>.</p><h3>The prerequisites</h3><ul><li>I have my Kohana-power site located under <code>/home/phoenixheart/www/my-kohana/</code> directory with proper permission set (owner being www-data, that is).</li><li>nginx has been set up properly and listening on port 80, with the configuration directory being <code>/etc/nginx/</code></li><li>I want my site to be locally accessible via my-kohana.dev. Any requests to www.my-kohana.dev should be permanently redirected to my-kohana.dev &#8211; which is also called &#8220;force non-www&#8221;.</li><li>I want to have neat URL rewriting without &#8220;index.php&#8221;, for example <code>index.php?controller=product&amp;function=get&amp;id=1</code> should be rewritten into <code>/product/get/1</code></li><li>I also want that all existing files and directories under the root directory are accessible, except Kohana&#8217;s system directories <code>system</code>, <code>application</code>, and <code>modules</code>. Any attempt to access system files and directories(beginning with dots, like .htaccess or .settings) should be disallowed also.</li></ul><p>All clear. So let&#8217;s do it!<br
/> <span
id="more-440"></span></p><h3>Set up the virtual host</h3><p>The way nginx handles virtual hosts is totally different from Apache, as we can expect. Instead of using .conf files to declare and configure the hosts, nginx, when started, additionally scans through the configuration folder (<code>/etc/nginx</code> in our case) to find (if any) configuration files under 2 directories: sites-available and sites-enabled. So under /etc/nginx/sites-available, create a file called my-kohana.dev with the following content:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">server <span style="color: #009900;">&#123;</span>
    listen   <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
    server_name my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">;</span>
&nbsp;
    access_log <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">/</span>access<span style="color: #339933;">.</span><span style="color: #990000;">log</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># remember to create this file
</span>    <span style="color: #990000;">error_log</span> <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">/</span>error<span style="color: #339933;">.</span><span style="color: #990000;">log</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># and this file
</span>
    location <span style="color: #339933;">/</span> <span style="color: #009900;">&#123;</span>
	root   <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">;</span>
	index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    location ~ \<span style="color: #339933;">.</span>php$ <span style="color: #009900;">&#123;</span>
	fastcgi_pass   127<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>1<span style="color: #339933;">:</span><span style="color: #cc66cc;">9000</span><span style="color: #339933;">;</span>
	fastcgi_index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
	fastcgi_param  SCRIPT_FILENAME  <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #000088;">$fastcgi_script_name</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">include</span>        fastcgi_params<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>Of course, this is just the basis configuration for the site to get up and running. To continue, we must create a host entry. Open /etc/hosts and add this line:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">127<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>1	my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev www<span style="color: #339933;">.</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev</pre></td></tr></table></div><p>If this was Apache, we would be good enough to restart the webserver to see the result. But like I said, nginx is different. For nginx to properly recognize and serve our site, we must <em>enable</em> the site by creating a symlink of the configuration file under <code>sites-enabled</code>. We do that as follow:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">ln <span style="color: #339933;">-</span>s <span style="color: #339933;">/</span>etc<span style="color: #339933;">/</span>nginx<span style="color: #339933;">/</span>sites<span style="color: #339933;">-</span>available<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev <span style="color: #339933;">/</span>etc<span style="color: #339933;">/</span>nginx<span style="color: #339933;">/</span>sites<span style="color: #339933;">-</span>enabled<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev</pre></div></div><p>Now, let&#8217;s restart nginx. Open Terminal and type:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">sudo <span style="color: #339933;">/</span>etc<span style="color: #339933;">/</span>init<span style="color: #339933;">.</span>d<span style="color: #339933;">/</span>nginx restart</pre></div></div><p>The result should be as followed:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">Restarting nginx<span style="color: #339933;">:</span> the configuration <span style="color: #990000;">file</span> <span style="color: #339933;">/</span>etc<span style="color: #339933;">/</span>nginx<span style="color: #339933;">/</span>nginx<span style="color: #339933;">.</span>conf syntax is ok
configuration <span style="color: #990000;">file</span> <span style="color: #339933;">/</span>etc<span style="color: #339933;">/</span>nginx<span style="color: #339933;">/</span>nginx<span style="color: #339933;">.</span>conf test is successful
nginx<span style="color: #339933;">.</span></pre></div></div><p>If you receive any &#8220;failure&#8221; response, chance is some typos in the configuration.</p><p>Now, http://my-kohana.dev should be accessible via the browser (Note that, if Kohana complains about the logs folder inaccessible, try properly setting its owner to www-data). Next step is tweaking the configuration a bit to serve our needs.</p><h3>Tweak it up</h3><h4>Force non-www</h4><p>To force non-www, open sites-available/my-kohana.dev and add these lines at the top:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">server <span style="color: #009900;">&#123;</span>
    listen <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
    server_name www<span style="color: #339933;">.</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">;</span>
    rewrite  ^<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$  http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//my-kohana.dev/$1  permanent;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>This kind of configuration is very similar to that of Apache, so I would assume there&#8217;s no need to explain. After a nginx restart, all request to http://www.my-kohana.dev should be silently redirected to http://my-kohana.dev.</p><h4>Neat URL rewriting</h4><p>Again, in sites-available/my-kohana.dev, modify the first <code>location</code> block to the following</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">location <span style="color: #339933;">/</span> <span style="color: #009900;">&#123;</span>
    root   <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">;</span>
    index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># this is where the rewriting gets done.
</span>    <span style="color: #666666; font-style: italic;"># refer to http://forum.kohanaphp.com/comments.php?DiscussionID=1505 for more info
</span>    rewrite ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.+</span><span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span>index<span style="color: #339933;">.</span>php?kohana_uri<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> last<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>But wait! We don&#8217;t want EVERY requests to be re-written. For example, a request to my-kohana.dev/css/style.css should be kept as-is. Same goes with javascripts and images. In short, if the request is for an existing file or folder, we keep it as-is; else, we route it to index.php using rewriting. To achieve that, modify the configuration above to this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">location <span style="color: #339933;">/</span> <span style="color: #009900;">&#123;</span>
    root   <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">;</span>
    index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>f <span style="color: #000088;">$request_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;"># translated into &quot;if the request is an existing file, break (do nothing)&quot;
</span>        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>d <span style="color: #000088;">$request_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;"># translated into &quot;if the request is an existing directory, break (do nothing)&quot;
</span>        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># the request is not an existing file or directory
</span>    <span style="color: #666666; font-style: italic;"># this is where the rewriting gets done.
</span>    <span style="color: #666666; font-style: italic;"># refer to http://forum.kohanaphp.com/comments.php?DiscussionID=1505 for more info
</span>    rewrite ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.+</span><span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span>index<span style="color: #339933;">.</span>php?kohana_uri<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> last<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><h4>Set files and folder accessibilities</h4><p>Now, we prohibit access to the sensitive stuffs, including kohana system folders, dot files and directories etc. It&#8217;s fairly simple with nginx. All we need to do is adding two more location blocks, specific for this purpose:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">location ~ <span style="color: #339933;">/</span>\<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># or, if you prefer
</span>    <span style="color: #666666; font-style: italic;">#return 403;
</span>    <span style="color: #666666; font-style: italic;"># or even
</span>    <span style="color: #666666; font-style: italic;">#deny all;
</span><span style="color: #009900;">&#125;</span>
location ~<span style="color: #339933;">*</span> ^<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>modules<span style="color: #339933;">|</span>application<span style="color: #339933;">|</span>system<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># or, if you prefer
</span>    <span style="color: #666666; font-style: italic;">#return 403;
</span>    <span style="color: #666666; font-style: italic;"># or even
</span>    <span style="color: #666666; font-style: italic;">#deny all;
</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>The final configuration file should look like this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">server <span style="color: #009900;">&#123;</span>
    listen <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
    server_name www<span style="color: #339933;">.</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">;</span>
    rewrite  ^<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$  http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//my-kohana.dev/$1  permanent;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
server <span style="color: #009900;">&#123;</span>
    listen   <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
    server_name my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">;</span>
&nbsp;
    access_log <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">.</span>access<span style="color: #339933;">.</span><span style="color: #990000;">log</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">error_log</span> <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">.</span>dev<span style="color: #339933;">.</span>error<span style="color: #339933;">.</span><span style="color: #990000;">log</span><span style="color: #339933;">;</span>
&nbsp;
    location ~ <span style="color: #339933;">/</span>\<span style="color: #339933;">.</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">404</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    location <span style="color: #339933;">/</span> <span style="color: #009900;">&#123;</span>
        root   <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #339933;">;</span>
        index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>f <span style="color: #000088;">$request_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;"># translated into &quot;if the request is an existing file, break (do nothing)&quot;
</span>            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>d <span style="color: #000088;">$request_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;"># translated into &quot;if the request is an existing directory, break (do nothing)&quot;
</span>            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># the request is not an existing file or directory
</span>        <span style="color: #666666; font-style: italic;"># this is where the rewriting gets done.
</span>        <span style="color: #666666; font-style: italic;"># refer to http://forum.kohanaphp.com/comments.php?DiscussionID=1505 for more info
</span>        rewrite ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.+</span><span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span>index<span style="color: #339933;">.</span>php?kohana_uri<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> last<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    location ~ \<span style="color: #339933;">.</span>php$ <span style="color: #009900;">&#123;</span>
        fastcgi_pass   127<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>0<span style="color: #339933;">.</span>1<span style="color: #339933;">:</span><span style="color: #cc66cc;">9000</span><span style="color: #339933;">;</span>
        fastcgi_index  index<span style="color: #339933;">.</span>php<span style="color: #339933;">;</span>
        fastcgi_param  SCRIPT_FILENAME  <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>phoenixheart<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>my<span style="color: #339933;">-</span>kohana<span style="color: #000088;">$fastcgi_script_name</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">include</span>        fastcgi_params<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    location ~<span style="color: #339933;">*</span> ^<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>modules<span style="color: #339933;">|</span>application<span style="color: #339933;">|</span>system<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">403</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>After another nginx restart, our Kohana instance should run without any hassles.</p><p>Typos? Mistakes? Errors? Let&#8217;s hear it in your comment.</p> <img
style='display:none' id="post-440-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2010/03/how-to-configure-nginx-to-run-kohana-on-ubuntu/',title:'How to configure nginx to run Kohana on Ubuntu',tweet:'As a web developer I&#8217;ve been using Apache for a long long time. Recently though, I&#8217;ve st',description:'As a web developer I&#8217;ve been using Apache for a long long time. Recently though, I&#8217;ve st'})"><script type='text/javascript'>document.getElementById("post-440-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2010/03/how-to-configure-nginx-to-run-kohana-on-ubuntu/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Social Sketches &#8211; a free icon set released for Six Revisions</title><link>http://www.phoenixheart.net/2010/01/social-sketches-a-free-icon-set-released-for-six-revisions/</link> <comments>http://www.phoenixheart.net/2010/01/social-sketches-a-free-icon-set-released-for-six-revisions/#comments</comments> <pubDate>Sun, 10 Jan 2010 02:08:30 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Design]]></category> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=435</guid> <description><![CDATA[Today I&#8217;m so pleased to announce the release of Social Sketches, my hand-drawn icon set exclusively done for Six Revisions. Initially it was made for Referrer Detector on my just-started sketch project The Daily Faces, but then I decided to make it available for public use, hence the featuring on Six Revisions yesterday. Here is [...]]]></description> <content:encoded><![CDATA[<p>Today I&#8217;m so pleased to announce the release of Social Sketches, my hand-drawn icon set exclusively done for Six Revisions. Initially it was made for Referrer Detector on my just-started sketch project <a
href="http://dai.lyfaces.com">The Daily Faces</a>, but then I decided to make it available for public use, hence <a
href="http://sixrevisions.com/freebies/icons/social-sketches-exclusive-free-hand-sketched-icon-set/">the featuring on Six Revisions</a> yesterday.</p><p>Here is the preview of the set:</p><p><img
class="shot" title="Social Sketches Preview" src="/wp-content/uploads/2010/01/social-sketches-preview.jpg" alt="" width="435" height="512" /></p><p>For more information and download, please head to <a
href="http://sixrevisions.com/freebies/icons/social-sketches-exclusive-free-hand-sketched-icon-set/">Six Revisions&#8217; post</a>.</p><p>P.S. I have a plan to add some more icons into the set, so stay tuned <img
src='http://www.phoenixheart.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p> <img
style='display:none' id="post-435-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2010/01/social-sketches-a-free-icon-set-released-for-six-revisions/',title:'Social Sketches &#8211; a free icon set released for Six Revisions',tweet:'Today I&#8217;m so pleased to announce the release of Social Sketches, my hand-drawn icon set exclus',description:'Today I&#8217;m so pleased to announce the release of Social Sketches, my hand-drawn icon set exclus'})"><script type='text/javascript'>document.getElementById("post-435-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2010/01/social-sketches-a-free-icon-set-released-for-six-revisions/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>WordPress: Thank that first time commentator!</title><link>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/</link> <comments>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/#comments</comments> <pubDate>Sat, 07 Nov 2009 05:12:08 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Blahblahblah]]></category> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[user interaction]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress hack]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=392</guid> <description><![CDATA[Thumbnail credit: Premshree Pillai To a website, comments are important &#8211; this you must agree. But not all visitors leave comments &#8211; in fact, very, very few. Most of them care about the content only, and tend to leave (bounce) the site right after getting the information they need (so sad a life, huh?) Many [...]]]></description> <content:encoded><![CDATA[<p><small>Thumbnail credit: <a
href="http://www.flickr.com/photos/premshree/">Premshree Pillai</a></small></p><p><span
class="drop-cap">T</span>o a website, comments are important &#8211; this you must agree. But not all visitors leave comments &#8211; in fact, very, very few. Most of them care about the content only, and tend to leave (bounce) the site right after getting the information they need (so sad a life, huh?)</p><p>Many tips have been introduced and used to encourage visitors to contribute to your site via comments. To my knowledge, and to name a few:</p><ul><li>Use dofollow links in comments. By default, WordPress and other blogging systems mark links in comments with <code>rel="nofollow"</code> attribute. This attribute tells search engines to not follow the links, which means the commenter&#8217;s site will not be able to share any Google juice with you. While effective in fighting spammers, this technique may a bit disappoint the real visitors. Plugins like <a
href="http://wordpress.org/extend/plugins/sem-dofollow/">Dofollow</a> address this problem and remove &#8220;nofollow&#8221; attribute from comment links.</li><li>Further promote the commenter&#8217;s blog (if any). <a
href="http://wordpress.org/extend/plugins/commentluv/">CommentLuv</a> is a plugin that &#8220;will visit the site of the comment author while they type their comment and retrieve a selection of their last blog posts, tweets or digg submissions which they can choose one from to include at the bottom of their comment when they click submit&#8221;.</li><li>Choose a (random) comment to give small prizes such as free ebooks, preminum themes etc.</li><li>Explicitly ask the readers to give comments at the end of the article &#8211; &#8220;Please share your thoughts&#8221;, &#8220;What do you think?&#8221;, &#8220;What say you?&#8221; etc. etc.</li></ul><p>Today I would like to mention another method to encourage commenting. Though this won&#8217;t likely attract more commenters, it may encourage existing ones to leave more comments and become more effective contributors.</p><p>The method is called &#8220;Thank first time commenters&#8221; and it works like this: <span
id="more-392"></span></p><p>Normally when a visitor leaves a comment for the first time on your blog, the comment is displayed under &#8220;awaiting moderation&#8221; status and visible to him only. When there&#8217;s nothing wrong with this approach, it&#8217;s not really interesting for the commenter himself. Now how about showing him a &#8220;thank you&#8221; or &#8220;welcome&#8221; message, or better, a whole page, for the first time contributing his [great] ideas to your site? This time it&#8217;s much cooler!</p><p>Let&#8217;s turn the idea into real action for a WordPress site.</p><h3>Step 1. Detect first time commenter</h3><p><img
class="shot" src="/wp-content/uploads/2009/11/comments.gif" alt="Comments" width="470" height="136" /><br
/> <small>Original image from <a
href="http://www.flickr.com/photos/chrismar/">Chrismar</a></small></p><p>To detect whether the comment author is leaving his first comment on you site, we need to <em>hook</em> into one of the comment-related functions (you can read about hooks <a
href="http://codex.wordpress.org/Plugin_API">here</a>). For this purpose I use <strike><code>comment_post()</code>, but there may be some other alternatives which I don&#8217;t know about</strike> <code>comment_post_redirect</code> filter. This filter is applied just before WordPress redirects the commentator after him posting a comment &#8211; exactly what we need.</p><p>In the theme&#8217;s <code>functions.php</code> page, add these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comment_post_redirect'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'check_first_time_comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * This functions accepts one parameter being the location to redirect commentators to
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> check_first_time_comment<span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the comment from global variables</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// if it's not &quot;unapproved&quot;, don't modify the redirect location</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_approved</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// now check if it's really the first time the commenter comments</span>
    <span style="color: #666666; font-style: italic;">// to do this, we check the number of comments this author have left on our site</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT COUNT(comment_ID) from <span style="color: #006699; font-weight: bold;">{$wpdb-&gt;prefix}</span>comments WHERE comment_author_email='<span style="color: #006699; font-weight: bold;">{$comment-&gt;comment_author_email}</span>'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// he's left more than 1 comment - do nothing with him</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//--- if we reach here, this is the first comment he leaves on our site. Do our stuffs now! ---//</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// first save the comment data into session. We'll use it later</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// now modify the $location so that WordPress redirects this commentator to our special page</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'/thank-you-for-contribution/'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>What the code does is pretty self-descriptive. The function <code>check_first_time_comment($location)</code> is triggered just before WordPress redirects the commentator to a location (being the post, the page, or the error page). It takes one parameter, being the location url. Upon triggered, it checks for the status of the comment and only does its deeds if the comment is &#8220;unapproved&#8221; (status code 0). Then, it does a further check to see if this is really the first time the comment author leaves a comment on the site with the email address. If this is true, it saves the comment data into session for later retrieval, and modifies the location string to redirect the commenter to the thank you page.</p><h3>Step 2. Create a &#8220;Thank you&#8221; landing page</h3><p><img
class="shot" src="/wp-content/uploads/2009/11/thanks-narrow.jpg" alt="Thank you!" width="470" height="130" /><br
/> <small>Original image by <a
href="http://www.flickr.com/photos/premshree/">Premshree Pillai</a></small></p><p>This is the page where our valuable commenter is redirected to. To create such a page, first copy page.php in the theme folder into thank-you-page.php. Open it and locate these lines on the top:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Template Name: Page
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>Change the template name to something descriptive like &#8220;Thank You Page&#8221;, and add some logic, like this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Template Name: Thank You Page
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// the page should not be directly accessed</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: /'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now get the comment data from session and utilize it</span>
<span style="color: #000088;">$first_time_comment</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// clear the session</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$the_post</span> <span style="color: #339933;">=</span> get_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$first_time_comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_post_ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the post of the comment</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// for some reason, we cannot find the post.</span>
    <span style="color: #666666; font-style: italic;">// The action here depends on you. For me I redirect to home.</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: /'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// tell the browser not to cache this page in anyway</span>
<span style="color: #666666; font-style: italic;">// !IMPORTANT: You may also want to exclude this page from cache plugins too!</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: no-cache, must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
&quot;Thank you&quot; content goes here
With the post on hand, you can enrich this thank you page with:
- similar posts
- posts in the same categories
- random posts
- most read posts
- most commented posts etc.
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>Save the template. Now go to WordPress control panel, create a new page with &#8220;Thank You Page&#8221; as the template, and <code>thank-you-for-contribution</code> as the slug. You may want to disable ping and comments for the page too.</p><p>There! Your blog is now ready to welcome the first time commentators!</p><p>But wait&#8230; there&#8217;s still a catch.</p><h3>Step 3. Post-production</h3><p>Now, if you list down your pages somewhere on your blog, &#8220;Thank You&#8221; page will stupidly appear. The fix is easy: <a
href="http://codex.wordpress.org/Template_Tags/wp_list_pages">exclude the page ID</a>. Also, if you have a cache plugin installed, it&#8217;s best to exclude Thank You page from caching.</p><p>Want to see it in action? Just leave a comment here as a new commentator!</p> <img
style='display:none' id="post-392-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/',title:'WordPress: Thank that first time commentator!',tweet:'Thumbnail credit: Premshree Pillai To a website, comments are important &#8211; this you must agree.',description:'Thumbnail credit: Premshree Pillai To a website, comments are important &#8211; this you must agree.'})"><script type='text/javascript'>document.getElementById("post-392-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>How I sped up my Thica.net</title><link>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/</link> <comments>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/#comments</comments> <pubDate>Tue, 27 Oct 2009 09:44:08 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Blahblahblah]]></category> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[optimization]]></category> <category><![CDATA[speed]]></category> <category><![CDATA[wordpress]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=377</guid> <description><![CDATA[Thumbnail credit: Amnemona If you didn&#8217;t notice, I have another site called Thica.net &#8211; Vietnam poetry network, a WordPress (what else) powered blog dedicated to poems in Vietnamese. The site is receiving about 60K of views per month, which is 12x to that of the moment when it was started back on March 2008, and [...]]]></description> <content:encoded><![CDATA[<p><small>Thumbnail credit: <a
href="http://www.flickr.com/photos/marinacvinhal/">Amnemona</a></small></p><p><span
class="drop-cap">I</span>f you didn&#8217;t notice, I have another site called <a
href="http://www.thica.net">Thica.net &#8211; Vietnam poetry network</a>, a WordPress (what else) powered blog dedicated to poems in Vietnamese. The site is receiving about 60K of views per month, which is 12x to that of the moment when it was started back on March 2008, and I&#8217;m rather happy about it.</p><p>About one month ago, Thica.net started to become very slow and tent to produce strange problems. More than often it threw 503 Internal Server Error just when I attempt to add a new post, or 404 Page Not Found for a page that I <em>knew</em> it was there, such as admin panel, plugin section etc. After some deep look inside, I decided that my site was too bloated and then it was time to optimize things to start it up. To admit, the result is nowhere near perfection, but it satisfies my need. So I think I&#8217;ll share with you here.</p><h4>1. Eliminate unused plugins</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/jigsaw.jpg" alt="Plugins" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/smackfu/">smackfu</a></p><p>Being a developer, I&#8217;m a big fan of plugins and addons. My Firefox has about 30 addons, ranging from <a
href="https://addons.mozilla.org/firefox/addon/1865">Adblock Plus</a> to <a
href="http://www.ultrareach.com/">UltraSurf</a> (I&#8217;m living in a communist country FYI) and <a
href="https://addons.mozilla.org/en-US/firefox/addon/5369">YSlow</a>. Similarly, Thica.net had like 50 plugins, active and inactive alike. So you know, plugins power up WordPress in many ways, but on the downside slow it down because of all the added functions, hooks, data and so on. Some plugins are even terribly written (like one random post plugin which gets <strong>ALL</strong> posts from the database and uses PHP loop to get 5 random posts &#8211; WTH) and may cause serious problems: slowness, security holes, or even crashes your site. <span
id="more-377"></span></p><p>So I dedicated my time filtering the plugins &#8211; which were <em>really</em> needed, which may be replaced by hacks and/or other inexpensive methods, which may be replaced with another plugin not as bloated, and which were totally useless. For instance, <a
href="http://wordpress.org/extend/plugins/akismet/">Askimet</a>, <a
href="http://wordpress.org/extend/plugins/ozh-admin-drop-down-menu/">Ozh&#8217; Admin Drop Down Menu</a>, <a
href="http://wordpress.org/extend/plugins/google-sitemap-generator/">Google XML Sitemaps</a>, and <a
href="http://wordpress.org/extend/plugins/login-lockdown/">Login LockDown</a> were definitely kept. I&#8217;m now using my own functions to get the random and most read posts, searching for a SEO plugin in place of <a
href="http://wordpress.org/extend/plugins/all-in-one-seo-pack/">All In One SEO Pack</a> as for some reasons <a
href="http://wordpress.org/extend/plugins/platinum-seo-pack/">Platinum SEO Pack</a> didn&#8217;t fit my needs, and have trashed away tens of unused plugins. By &#8220;trashing away&#8221;, I mean deleting from the hard drive, as inactive plugins still waste WP resource &#8211; it still has to look them up and check for their status anyway.</p><h4>2. Use a total cache solution</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/db.jpg" alt="Database" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/adesigna/">adesigna</a></p><p>To a busy application &#8211; web or non-web alike, caching is vital. For as far as I know WordPress is not as bloated as other CMS (Joomla, Drupal, Magento to name a few), but its performance has rooms to be improved. I had been using <a
href="http://wordpress.org/extend/plugins/wp-super-cache/">WP SuperCache</a> for quite a while and the performance was good enough, until the problems occurred and I realized that it had some extra features that I didn&#8217;t need at all. Then I sought out for some alternatives, like <a
href="http://wordpress.org/extend/plugins/w3-total-cache/">W3 Total Cache</a>, <a
href="http://wordpress.org/extend/plugins/askapache-crazy-cache/">Crazy Cache</a> etc. <a
href="http://www.satollo.net/plugins/hyper-cache">HyperCache</a> is where I stopped at &#8211; just a matter of personal opinion, and I&#8217;m happy with it so far.</p><p>But HyperCache alone was not enough in my case. Don&#8217;t you know that for a complete page to be served, WordPress has to make like a bunch of continuous calls to the database? My Thica.net for instance, makes an average of 50 MySQL queries for the home page and 80 to 90 queries for each single page. That&#8217;s rather expensive isn&#8217;t it?</p><p>Lucky me, there&#8217;s another kind of caching solution called database caching. As most of the query results don&#8217;t change from time to time &#8211; the post content, the categories, the tags&#8230; often remain the same &#8211; they can be cached for later use. For this purpose I installed <a
href="http://wordpress.org/extend/plugins/db-cache-reloaded/">DB Cache Reloaded</a> and &#8211; just like magic &#8211; almost all of the queries (85 over 90 for example) are now served from the cache. You must admit, that&#8217;s a huge improvement. My host should thank me for not bombing their server!</p><h4>3. Staticalize WordPress variables</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/phpcode.jpg" alt="Plugins" width="470" height="136" /></p><p>WordPress is so flexible &#8211; almost anything can be customized. That&#8217;s definitely great. But sometimes the greatness comes a bit too far from necessity. If you open a normal header.php file from a normal theme, 99% chance is you&#8217;ll see these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> &lt;?php language_attributes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>&gt;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php bloginfo('html_type'); ?&gt;</span></span>; charset=<span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'charset'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>&quot; /&gt;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;?php wp_title<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&amp;laquo;'</span>, true, <span style="color: #ff0000;">'right'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span> <span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'stylesheet_url'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pingback&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php bloginfo('pingback_url'); ?&gt;</span></span>&quot; /&gt;</pre></td></tr></table></div><p>You guessed it, these scripts get the settings from the database. Now let&#8217;s face it: how often would you change your blog name, or the language, or the style sheet URL, or the HTML charset? For me, it&#8217;s once and for all. vi-VN is my site lang, charset is of course UTF-8, and I don&#8217;t have any intention to change them ever. That&#8217;s why in the theme, I replaced the dynamic scripts with static content whenever applicable:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color: #000066;">dir</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ltr&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;vi-VN&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://static.thica.net/css/notepad-chaos.css?ver=1.2.21&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;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pingback&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.thica.net/xmlrpc.php&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- and so forth --&gt;</span></pre></td></tr></table></div><p>This way I saved quite a few of unnecessary queries and made my site run a bit faster.</p><h4>4. Follow Yahoo&#8217;s optimization tips</h4><p>Have you taken a look at Yahoo!&#8217;s <a
href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a>? It is an awesome resource for those who want to optimize their blogs for performance. Among the rules, these are particularly useful for my case and have been implemented:</p><h5>4.1. Add an Expires or a Cache-Control Header</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/clock.jpg" alt="Clock" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/laffy4k/">laffy4k</a></p><p>An Expires header lets the browser have an idea of when the requested content is expired and needs to be re-downloaded. By setting a content&#8217;s expires header to a far future, you can tell the browser to use the local cached copy instead of retrieving a fresh version from the server, thus save both bandwidth and loading time. Perfect candidates for this are the images, css, javascript etc.</p><p>In the .htaccess file of Thica.net&#8217;s root directory, I added these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;IfModule mod_expires.c&gt;</span>
ExpiresActive on
ExpiresByType image/gif &quot;access plus 10 years&quot;
ExpiresByType image/png &quot;access plus 10 years&quot;
ExpiresByType image/jpg &quot;access plus 10 years&quot;
ExpiresByType application/x-javascript &quot;access plus 10 years&quot;
ExpiresByType text/css &quot;access plus 10 years&quot;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>IfModule&gt;</span></pre></td></tr></table></div><p>These lines mean &#8220;These images, javascript, and css includes will not be changed for 10 years more, so dear Firefox/Chrome/Opera/Safari/IE, please use their copies from your cache whenever applicable and don&#8217;t put the heavy load on me, thanks&#8221;. The so understanding browser will be ok with such a polite request, and tada, all the specified contents are loaded from the cache in a blink of an eye.</p><h5>4.2 Gzip Components</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/zip.jpg" alt="Zip" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/xploded/">Isobel T</a></p><p>Now-a-days, all modern browsers support gzipped components. Imagine it like this: first the browser requests for a page. Instead of returning the page as is, the server compresses it and sends the archive back. The browser receives the archive, decompresses it into normal state, and renders the decompressed content normally. Like WinZIP or WinRAR does, but in a web context. Simple?</p><p>To take advantage of this technique, once again I opened the .htaccess file and added these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;ifmodule mod_deflate.c&gt;</span>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>ifmodule&gt;</span></pre></td></tr></table></div><p>This configuration forces the server to gzip those MIME contents before returning them to the browser. Made a quick test at the <a
href="http://www.gidnetwork.com/tools/gzip-test.php">GIDZipTest</a> fot the homepage and I received this result:</p><table
style="height: 250px; width: 300px;" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td
style="border-bottom: 1px dotted #dadada;">Web page compressed?</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;"><span
style="color: green;">Yes</span></td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Compression type?</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">gzip</td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Size, Markup (bytes)</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">31,179</td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Size, Compressed (bytes)</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">7,476</td></tr><tr><td>Compression %</td><td
style="text-align: right; font-weight: bold;">76.0</td></tr></tbody></table><p>So, 3 lines added into the .htaccess file and I reduced 76% of bandwidth (means loading time cut down to 24%). Marvelous.</p><h5>4.3 Split Components Across Domains</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/server.jpg" alt="Servers" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/jamisonjudd/">Jamison_Judd</a></p><p>Browsers have a limit on how many parallel requests can be sent &#8211; it&#8217;s 2 for Internet Explorer and 4 for Firefox (configurable, but can&#8217;t exceed 8 if I&#8217;m not wrong). This is a per domain value. It is advised that a site&#8217;s components should be distributed on more than one domain to maximize the parallel downloads. So if your HTML is served from site.com, your images are located on img.site.com, and your css files are put on static.site.com, the number of maximum parallel downloads a browser can perform on your site are tripled, and the loading time may be cut by two third or so.</p><p>That&#8217;s why I created two subdomains: img.thica.net for the theme images, and static.thica.net for other static contents like style sheets and javascripts. The performance is significantly improved &#8211; my site often finishes loading before I knew it, period.</p><h4>The result so far</h4><p>Well, my site ran much faster. Unfortunately, the errors kept occurring from time to time still. From the error logs, it showed that somehow my php5 CGI processes were terminated now and then, hence the 503 and 404 errors.</p><p>I contacted my host. They said my site was using too much of resource. I told them that a WP-powered site with 2K to 3K of views per day and 2GB of bandwidth a month could hardly use too much resource. So they were like: bandwidth and resource are not the same. Then they blamed my PHP scripts, saying that All in One SEO Pack sucked. &#8220;Then how come it&#8217;s so widely used around this earth?&#8221; I asked. They kept silence, the errors kept happening.</p><p>I was forced to use my final weapon.</p><h4>5. Switch to another host</h4><p><a
href="http://wpwebhost.com/affiliate/idevaffiliate.php?id=728_0_1_17"><img
class="shot" src="/wp-content/uploads/2009/10/wpwebhost.jpg" alt="WPWebHost" width="470" height="136" /></a></p><p>Yes, the final step I took was switching to another host. I asked for reference from many people, and later got convinced by Jean Baptiste Jung, the famous guy behind <a
href="http://www.wprecipes.com">WPRecipes</a>, <a
href="http://www.catswhocode.com">CatsWhoCode</a>, and most recently <a
href="http://www.codeswhoblog.com">CatsWhoBlog</a>, to go with <a
href="http://www.wpwebhost.com">WPWebHost</a> (many thanks, Jean!). It was not a smooth migration to be honest, as I had 4 or 5 sites to be relocated, when my old host doesn&#8217;t use cPanel for site controlling. But what I do really, really appreciate is, no matter what the problem was, no matter small or big, they were always there, supportive and helpful.</p><p>Well I don&#8217;t want to sound like a salesman, but WPWebHost really rocks. So if you are planning for a move, I highly recommend them. The banner above is in fact an affiliate link, so I would appreciate a lot if you purchase their hosting package via clicking on it <img
src='http://www.phoenixheart.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><h4>The <em>final</em> result</h4><p>That&#8217;s it &#8211; my journey to optimize my Thica.net to make it speedy, and I&#8217;m really pleased with the result: faster load, light footprints, no stupid errors, minimum bandwidth. You may want to check the result yourself at <a
href="http://www.thica.net" title="Thica.net - Mạng thi ca Việt Nam">the site itself</a>. What do you think about it / this article / my pidgin English / my bad writing? I&#8217;m happy to see your comments.</p> <img
style='display:none' id="post-377-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/',title:'How I sped up my Thica.net',tweet:'Thumbnail credit: Amnemona If you didn&#8217;t notice, I have another site called Thica.net &#8211; ',description:'Thumbnail credit: Amnemona If you didn&#8217;t notice, I have another site called Thica.net &#8211; '})"><script type='text/javascript'>document.getElementById("post-377-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Here we go &#8211; CDN Rewrites</title><link>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/</link> <comments>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/#comments</comments> <pubDate>Thu, 24 Sep 2009 10:15:34 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[cdn-rewrites]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[wordpress]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=325</guid> <description><![CDATA[Right after Free CDN was released, I got a request to enhance the plugin to support commercial Content Delivery Networks &#8211; you know, those big guys like Akamai, Limelight, EdgeCast, Velocix etc. The implementation is not too complicated: specify an origin host, and rewrite it into a destination host. That origin is of course usually [...]]]></description> <content:encoded><![CDATA[<p>Right after Free CDN was released, I got a request to enhance the plugin to support commercial Content Delivery Networks &#8211; you know, those big guys like Akamai, Limelight, EdgeCast, Velocix etc. The implementation is not too complicated: specify an origin host, and rewrite it into a destination host. That origin is of course usually http://www.a-busy-site.com, and the destination is something a <a
href="http://www.valuecdn.com" class="qc">Content Delivery Network</a> would provide you with: http://static.a-busy-site.com, or http://images.a-busy-site.com, or http://a-static-host.com etc. This way, all static contents will be served from that CDN host.</p><p>So, instead of developing the enhancement as a new feature for Free CDN, I decided to create a new plugin called CDN Rewrites. <span
id="more-325"></span>The implementation was not really a breeze, but in the end it worked rather smoothly.</p><p>I would like to thank <a
href="http://www.ezsite.us/">Mike Colburn</a> for the idea and being so kind enough to help me with all the documents and testing. Without him this plugin would never see the light.</p><p>Head <a
href="/wp-plugins/cdn-rewrites/">here for the plugin&#8217;s official page</a>.</p> <img
style='display:none' id="post-325-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/',title:'Here we go &#8211; CDN Rewrites',tweet:'Right after Free CDN was released, I got a request to enhance the plugin to support commercial Conte',description:'Right after Free CDN was released, I got a request to enhance the plugin to support commercial Conte'})"><script type='text/javascript'>document.getElementById("post-325-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>First version of Free CDN WP plugin released!</title><link>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/</link> <comments>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/#comments</comments> <pubDate>Sun, 13 Sep 2009 17:03:10 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[free CDN]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[wordpress]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=314</guid> <description><![CDATA[Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves me so little free time for other hobbies &#8211; I&#8217;m talking about my books, my December Flower guitar self-training, my photographic stuffs etc. On the other hand, it does improve my knowledge and skills with all those working [...]]]></description> <content:encoded><![CDATA[<p>Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves me so little free time for other hobbies &#8211; I&#8217;m talking about my books, my <a
href="http://www.youtube.com/watch?v=sLPXAcAsLgo">December Flower</a> guitar self-training, my photographic stuffs etc. On the other hand, it does improve my knowledge and skills with all those working requirements.</p><p>I&#8217;m rather lucky to be working as an R&amp;D guy in my current company, thus got a (legal) chance to (legally) spare a lot of time for (sometimes illegal) new and cool stuffs. Among them is <abbr
title="Content Delivery Network">CDN</abbr>, a solution to distribute (mostly static) contents across a network and let end-user access their copies from the cloud instead of the central server itself, thus reduces bottle neck problems during peak hours. To enterprise websites like those of Microsoft, Yahoo, Amazon, eBay etc., this is vital, as the number of concurrent visitors and downloads very frequently exceeds millions. Some of them build their own CDN, when the others rather hire third party services to handle the load to save time and money. Most well known among these 3rd services are properly <a
href="http://www.akamai.com">Akamai</a> and <a
href="http://www.limelightnetworks.com/">Limelight</a>, though there is a vast of them, naturally. For instance, Windows 7 downloads (~2GB each!) were served through Akamai network, when the live internet broadcast of Barrack Obama&#8217;s inaugural speech was done with help from Limelight.<span
id="more-314"></span></p><p>CDN&#8217;s are great as they save websites from heavy loads thus reduces bandwidth and shorten that number on the webmasters&#8217; monthly bills. So why aren&#8217;t they used so popularly? Well, because in general they are expensive, as you can tell. Depending on each CDN&#8217;s price table and the data size, the cost of using a CDN may range from thousands a month, which is unaffordable for like 98% of our blog owners despite of being just a peanut to Yahoo and Microsoft. So unless he is earning millions from selling Photoshop brushes or Google ads on his personal blog, a blog owner would close his eyes and wait for that Digg wave to calm down instead of (dare) using a CDN and survive the typhoon. So the rich keep getting richer and the poor keep remaining poor, life is bitter huh?</p><p>Not really (whoo hoo!). Akamai and Limelight are big, but they are not the only. Aside of those commerical CDN&#8217;s, there are some free ones, like <a
href="http://www.coralcdn.org/">Coral</a>, which provide us with free CDN &#8211; means free bandwidth. Coral CDN uses peer-to-peer technology in its CDN architecture, thus eliminates the need of maintaining a system of expensive server clusters while still offering an CDN solution with acceptable speed and stability. Long story shot, we small blog owners can also have CDN (on pair with Microsoft, yay!).</p><p>It&#8217;s dead simple to get a content (being any of types, but most commonly static contents like images, video clips, music tracks, css, java scripts) served thought Coral. All you have to do is appending <em
style="color:#f00; font-weight:bold">.nyud.net</em> to the host name of that content&#8217;s URL and call it done. Try it <a
href="http://www.phoenixheart.net.nyud.net">here</a>, <a
href="http://www.thica.net.nyud.net">here</a>, and <a
href="http://www.digg.com.nyud.net">here</a> to see it yourself. The first time requested, the content is retrieved from the original server and cached on Coral&#8217;s distributed servers. From the second request on, visitors will get a copy of that content from Coral. If the content weights 100KB in size, you&#8217;ve just saved 100KB bandwidth. If you have 1000 requests, it&#8217;s 100MB of bandwidth saved. Piece of cake.</p><p>So I was very excited getting to know about Coral and free P2P CDN&#8217;s. Then I told myself: ok, it&#8217;s time for another WordPress plugin &#8211; a &#8220;Free CDN&#8221; one which will rewrite all static URLs (being images, css, js etc.) to take advantage of Coral. Easier said than done, but nonetheless, I managed to get it done (thank you very much <a
href="http://omninoggin.com/">Thaya</a>, for the great help on how to capture the whole WordPress HTML stream). Today is the day <a
href="/wp-plugins/free-cdn/">Free CDN WordPress plugin</a> is born, cheers! The concept behind this plugin is simple: it looks for static contents inside a WordPress page and rewrites them into Coral-ready format. For example, <em>http://www.yourblog.com/wp-contents/themes/one-room/images/sprites.png</em> will be rewritten into <em>http://www.yourblog.com<strong>.nyud.net</strong>/wp-contents/themes/one-room/images/sprites.png</em> and gets handled by Coral network.</p><p>If your blog is rather small and you don&#8217;t have to worry about bandwidth fee / peak times, then it may be not worth your time to lay an eye on this plugin. But if you have a big enough blog with tens of thousands of views a day, and the bandwidth fee is giving you some good nightmare, and the bottle necks are causing you some headaches, then why not <a
href="/wp-plugins/free-cdn/">give Free CDN a try</a>?</p> <img
style='display:none' id="post-314-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/',title:'First version of Free CDN WP plugin released!',tweet:'Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves m',description:'Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves m'})"><script type='text/javascript'>document.getElementById("post-314-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>Write your own URL shortener</title><link>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/</link> <comments>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/#comments</comments> <pubDate>Thu, 04 Jun 2009 11:53:37 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[twitter]]></category> <category><![CDATA[url shorten]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=305</guid> <description><![CDATA[With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen TinyURL in the past, now Bit.ly, Is.gd, Tr.im and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for ➡.ws for its [...]]]></description> <content:encoded><![CDATA[<p>With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen <a
href="http://tinyurl.com">TinyURL</a> in the past, now <a
href="http://bit.ly">Bit.ly</a>, <a
href="http://is.gd">Is.gd</a>, <a
href="http://tr.im">Tr.im</a> and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for <a
href="http://➡.ws">➡.ws</a> for its cool name). With that being said however, it is interesting how they are doing it out there &#8211; what mechanism / algorithm / buzzword-here?</p><p>I curiously did some &#8220;research&#8221;es (well, another buzzword), and it seems in order to create the shorten URLs they are following the same steps:</p><ol><li>Insert the original (long) URL into the database</li><li>Get the insert ID. If the URL already exists, take its row ID. Let&#8217;s say we got 123456.</li><li>Convert the ID 123456 into something even shorter, let&#8217;s say &#8220;am4k&#8221;</li><li>Make use of Apache&#8217;s mod_rewrite so that any request to <em>http://host.com/am4k</em> will reach <em>http://host.com/redir_script.php?code=am4k</em> instead.</li><li>In redir_script.php the value &#8220;am4k&#8221; is converted back into base10&#8242;s 123456 and the corresponding URL is queried back from database</li><li>If a URL is found, redirect the request to it.</li></ol><p>It is simple enough&#8230; except for step 3 and 4. Which conversion is there, and how is the .htaccess written? <span
id="more-305"></span></p><p>So I did some other <em>buzzword-here</em>, and it turned out <a
href="http://en.wikipedia.org/wiki/Base36">base36 encoding</a> is taking place. According to Wikipedia, &#8220;the choice of 36 is convenient in that the digits can be represented using the <a
class="mw-redirect" title="Hindu-Arabic numerals" href="http://en.wikipedia.org/wiki/Hindu-Arabic_numerals">Arabic numerals</a> 0-9 and the <a
title="Latin alphabet" href="http://en.wikipedia.org/wiki/Latin_alphabet">Latin letters</a> A-Z&#8221;. There is a handy conversion table too:</p><table
style="border:1px solid #ccc"><tbody><tr
align="right"><th>Decimal</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td></tr><tr
align="right"><th>Base 36</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td></tr><tr><td
colspan="19"></td></tr><tr
align="right"><th>Decimal</th><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td>32</td><td>33</td><td>34</td><td>35</td></tr><tr><th>Base 36</th><td>I</td><td>J</td><td>K</td><td>L</td><td>M</td><td>N</td><td>O</td><td>P</td><td>Q</td><td>R</td><td>S</td><td>T</td><td>U</td><td>V</td><td>W</td><td>X</td><td>Y</td><td>Z</td></tr></tbody></table><p>Even better, PHP takes only one line of code to convert between decimal and base36, as shown on the same Wiki page:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base_36</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ZAQFG&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Base 36 Number</span>
<span style="color: #000088;">$decimal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;7654321&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Decimal Number</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $base_36 converted to decimal</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decimal</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $decimal converted to base 36</span></pre></td></tr></table></div><p>In our example, <em>123456</em> will be converted into its tidy equivalent base36 <em>2n9c</em> (not <em>am4k</em>, my very bad).</p><p>As for the mod_rewrite thing, as I am not a .htaccess master, I copied this thing that has been working perfectly so far from <a
href="http://www.codeigniter.com">CodeIgniter</a> forum:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">RewriteEngine On
RewriteBase <span style="color: #339933;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Checks to see if the user is attempting to access a valid file,
</span><span style="color: #666666; font-style: italic;">#such as an image or css document, if this isn't true it sends the
</span><span style="color: #666666; font-style: italic;">#request to redir.php.
</span>RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ redir<span style="color: #339933;">.</span>php?code<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span>
&nbsp;</pre></td></tr></table></div><p>With this, all of the hardest parts are done, leave the remains being simple MySQL database, queries and HTML form handling. To save you from getting boring, I&#8217;ll omit it.</p><p><a
href="http://u.phoenixheart.net/" class="demo">Instead, let&#8217;s see a demo here</a><a
href="http://u.phoenixheart.net/source.zip" class="download" rel="nofollow">The source code of the demo are can be downloaded from the same server</a><br
/> <br
class="clear" /></p> <img
style='display:none' id="post-305-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/',title:'Write your own URL shortener',tweet:'With Twitter and its 140 characters limit leading all the trends, URL shortening services are in ext',description:'With Twitter and its 140 characters limit leading all the trends, URL shortening services are in ext'})"><script type='text/javascript'>document.getElementById("post-305-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Referrer Detector 4.0 is out!</title><link>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/</link> <comments>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/#comments</comments> <pubDate>Tue, 12 May 2009 16:51:48 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[Referrer Detector]]></category> <category><![CDATA[wordpress]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=290</guid> <description><![CDATA[Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a major version increment because of these reasons: The code has been COMPLETELY re-written from scratch. As of the previous versions, it was one big file that handled everything from admin to front-end control. Needless to say how [...]]]></description> <content:encoded><![CDATA[<p>Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a major version increment because of these reasons:</p><ol><li>The code has been COMPLETELY re-written from scratch. As of the previous versions, it was one big file that handled everything from admin to front-end control. Needless to say how inconvenient this approach had become when bug fixes and new features were added&#8230; too bad that I have decided to throw all away and build a brand new <abbr
title="Object Oriented Programming">OOP</abbr> Referrer Detector. Well, it was a looong and tough way, but I&#8217;ve never looked back!</li><li>The data are now in (ahem) database. I was thinking (and convincing myself) that a JavaScript file is faster, as it reduces the number of database requests. But with time, it becomes too bloat and too hard for me to track bugs as well as to add improvements. So I told myself: hell with this sacrifice, I better obey the power of MySQL.</li><li>The biggest new feature that I&#8217;m really excited of is the ability to add <em>localized messages</em>. In the past, your users were welcomed the the same (English) greetings regardless of which country they were from. Now you can specify a localized message for those from Vietnam, another for Brazilians, Portuguese, and so on. The plugin will try to detect users&#8217; country and decide which message to show. Isn&#8217;t it cool?</li><li>The second new feature is the ability to backup and restore stuffs, including entries, excluded URLs, and options. For restoring, in order to keep the administration panel AJAX&#8217;ed, I go with <a
href="http://www.uploadify.com/">Uploadify</a>, a wonderful jQuery file upload plugin. This plugin uses a bit of Flash, but no worries, it will still works if your browser has no Flash player installed.</li><li>For the Stats panel, there were two problems that caused me much of headache. One is Google TLDs which are hundreds in number thus totally ruined the chart. The other is the chart itself: PHP/SWF Charts library is too darn heavy and often broke my SVN commits. So I wrote some code to group those annoying TLDs into one group, and use <a
href="http://code.google.com/apis/chart/">Google Chart</a> instead.</li><li>In the admin panel, I added a &#8220;Support this plugin!&#8221; tab. Just a bit about myself, like &#8220;Follow me on Twitter&#8221;. Hope you aren&#8217;t pissed of with this change.</li></ol><p>As usual, the plugin is downloadable at <a
href="http://wordpress.org/extend/plugins/referrer-detector/">WordPress Codex</a>. Your comments are always welcome here and in the <a
href="/wp-plugins/referrer-detector/">plugin page</a>. Let me know if you&#8217;re happy with the new version, or about the bugs you encounter!</p> <img
style='display:none' id="post-290-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/',title:'Referrer Detector 4.0 is out!',tweet:'Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a ma',description:'Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a ma'})"><script type='text/javascript'>document.getElementById("post-290-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Code snippet #2 &#8211; Shorten Links Using Bit.ly</title><link>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/</link> <comments>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/#comments</comments> <pubDate>Sat, 09 May 2009 02:29:26 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[bit.ly]]></category> <category><![CDATA[snippet]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=285</guid> <description><![CDATA[Following the first snippet, here is another small one to shorten a link using Bit.ly. I chose Bit.ly over other link shortening services due to a simple reason: Twitter uses it as default (by now). Code snippet #2 &#8211; Shorten Links Using Bit.ly Purpose Create a shortened URL using Bit.ly. This is extremely useful for [...]]]></description> <content:encoded><![CDATA[<p>Following <a
href="/2009/05/code-snippet-1-get-latest-tweet/">the first snippet</a>, here is another small one to shorten a link using <a
href="http://bit.ly">Bit.ly</a>. I chose Bit.ly over other link shortening services due to a simple reason: Twitter uses it as default (by now).</p><h3>Code snippet #2 &#8211; Shorten Links Using Bit.ly</h3><h3>Purpose</h3><p>Create a shortened URL using Bit.ly. This is extremely useful for your visitors, as they don&#8217;t have to look any further &#8211; just grab it to use right away.</p><h3>Requirements</h3><ul><li>A Bit.ly account (and API key). You can <a
href="http://bit.ly/account/register?rd=/">create one for free</a> at their site in seconds. Once logged in, head to Account section and look for a long ugly string starting with &#8220;R_&#8221;.</li><li>PHP 5 &gt;= 5.2.0. Your host doesn&#8217;t support PHP 5? Claim your money back and find another host.</li></ul><h3>The Code</h3><p><span
id="more-285"></span></p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_shortened_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$bitly_api_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR KEY HERE'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$bitly_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR BIT.LY USERNAME HERE'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://api.bit.ly/shorten?version=2.0.1&amp;longUrl=<span style="color: #006699; font-weight: bold;">$url</span>&amp;login=<span style="color: #006699; font-weight: bold;">$bitly_username</span>&amp;apiKey=<span style="color: #006699; font-weight: bold;">$bitly_api_key</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorCode</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shortUrl</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now use it</span>
<span style="color: #b1b100;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com/this-is-a-stupid-long-long-long-url.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// how about WordPress?</span>
<span style="color: #666666; font-style: italic;">// Just place the function above somewhere in your functions.php theme page and then</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 1. inside The Loop</span>
<span style="color: #b1b100;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 2. outside The Loop</span>
<span style="color: #b1b100;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;</pre></td></tr></table></div><img
style='display:none' id="post-285-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/',title:'Code snippet #2 &#8211; Shorten Links Using Bit.ly',tweet:'Following the first snippet, here is another small one to shorten a link using Bit.ly. I chose Bit.l',description:'Following the first snippet, here is another small one to shorten a link using Bit.ly. I chose Bit.l'})"><script type='text/javascript'>document.getElementById("post-285-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Code snippet #1 &#8211; Get Latest Tweet</title><link>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/</link> <comments>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/#comments</comments> <pubDate>Wed, 06 May 2009 12:29:41 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[snippet]]></category> <category><![CDATA[twitter]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=271</guid> <description><![CDATA[Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well as his blog address) to try to post something useful even when I don&#8217;t have anything to blog about. How about a series of code snippets? To some they are old but to the others they may help &#8211; for [...]]]></description> <content:encoded><![CDATA[<p>Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well as his blog address) to try to post something useful even when I don&#8217;t have anything to blog about. How about a series of code snippets? To some they are old but to the others they may help &#8211; for example to myself two months later that is.</p><div
class="snipper-name"><h3>Code Snippet #1 &#8211; Get Latest Tweet</h3></div><div
class="snipper-purpose"><h3>Purpose</h3><ul><li>Get your latest tweet from Twitter to show up instead of an unconvincing &#8220;Follow Me&#8221; link -or-</li><li>Display your idol&#8217;s latest tweet to wow your readers</li></ul></div><div
class="snipper-require"><h3>Requirements</h3><ul><li>A Twitter account. Don&#8217;t have one? Head <a
href="http://twitter.com/signup">here</a> or <a
href="http://www.iliveunderarock.com/">here</a></li></ul></div><p><span
id="more-271"></span></p><div
class="snipper-code"><h3>The Code</h3><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the function</span>
<span style="color: #009933; font-style: italic;">/**
 * @desc Get latest tweet from a Twitter account
 * @param string The account's username
 * @return string The tweet
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> get_latest_tweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://search.twitter.com/search.atom?q=from:<span style="color: #006699; font-weight: bold;">$username</span>&amp;rpp=1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;content type=&quot;html&quot;&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;/content&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">html_entity_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now use it</span>
<span style="color: #000088;">$my_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Phoenixheart'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> get_latest_tweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div></div> <img
style='display:none' id="post-271-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/',title:'Code snippet #1 &#8211; Get Latest Tweet',tweet:'Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well a',description:'Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well a'})"><script type='text/javascript'>document.getElementById("post-271-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 3/11 queries in 0.022 seconds using disk

Served from: www.phoenixheart.net @ 2010-09-09 08:09:16 -->