<?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>Devlog</title>
	<atom:link href="http://devlog.info/feed/" rel="self" type="application/rss+xml" />
	<link>http://devlog.info</link>
	<description>One developers blog.</description>
	<lastBuildDate>Wed, 01 Jun 2011 00:29:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quickie: Restoring a deleted MySQL root user</title>
		<link>http://devlog.info/2010/10/12/restoring-a-deleted-mysql-root-user/</link>
		<comments>http://devlog.info/2010/10/12/restoring-a-deleted-mysql-root-user/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 21:00:26 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Quickie]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[root]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=153</guid>
		<description><![CDATA[I accidentally deleted my MySQL root user today on one of my dev servers. I thought I'd write a quickie about how I restored it.
The basic information I gathered from the MySQL manual, but of course I needed to restore the account, not just reset the password. It's a simple matter of replacing that init [...]]]></description>
			<content:encoded><![CDATA[<p>I accidentally deleted my MySQL root user today on one of my dev servers. I thought I'd write a quickie about how I restored it.<span id="more-153"></span></p>
<p>The basic information I gathered from the <a href="http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html">MySQL manual</a>, but of course I needed to restore the account, not just reset the password. It's a simple matter of replacing that init file with the proper SQL to create the user.</p>
<pre id="raw-code-2" style="display:none; width: 1px; height: 1px; overflow: hidden;">root# /etc/init.d/mysqld stop
root# su mysql
mysql$ pwd
/home/mysql
mysql$ echo &quot;GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'my_pass' WITH GRANT OPTION;&quot;&gt; mysql-init
mysql$ mysqld_safe --init-file=/home/mysql/mysql-init &amp;
mysql$ rm mysql-init
mysql$ mysql -uroot -p   # woot, works</pre>
<div class="igBar">
<div class="wrap"><span id="lcode-2" style="float:right"><a href="#" onclick="javascript:showCodeTxt('code-2'); return false;">Plain Text</a></span><span class="langName">CODE:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="code-2">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">root# /etc/init.<span style="">d</span>/mysqld stop</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">root# su mysql</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql$ pwd</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">/home/mysql</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql$ echo <span style="color:#CC0000;">"GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'my_pass' WITH GRANT OPTION;"</span>&gt; mysql-init</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql$ mysqld_safe --init-file=/home/mysql/mysql-init &amp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql$ rm mysql-init</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mysql$ mysql -uroot -p&nbsp; &nbsp;# woot, works </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>(Note that it's important that you switch to the same user that MySQL uses when running mysqld_safe. This might be 'mysql' or something else, you'll need to check out your init files to see.)</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2010/10/12/restoring-a-deleted-mysql-root-user/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cross-domain AJAX with JSONP</title>
		<link>http://devlog.info/2010/03/10/cross-domain-ajax/</link>
		<comments>http://devlog.info/2010/03/10/cross-domain-ajax/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 23:42:54 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=139</guid>
		<description><![CDATA[Anyone who develops Javascript long enough undoubtedly runs into difficulties involving the various security features all browser vendors implement. These security features are a good thing -- they protect us from malicious users hijacking our browsing experience. But they can certainly cause some headaches. The security feature that presents the most difficulty for us as [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who develops Javascript long enough undoubtedly runs into difficulties involving the various security features all browser vendors implement. These security features are a good thing -- they protect us from malicious users hijacking our browsing experience. But they can certainly cause some headaches. The security feature that presents the most difficulty for us as developers is the <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>.</p>
<p>In a nutshell, this policy prevents pages from two different domains from modifying each others properties, using XMLHttpRequest, setting cookies etc. For instance, Example.com and OtherExample.com can't get references to each others <code>document</code> properties and can't set cookies on each other. Additionally, Example.com can't use XMLHttpRequest (aka AJAX) to load a resource from OtherExample.com. This last bit is probably the biggest issue for developers today -- in todays world of open web services and mashups. How do you consume a web service with Javascript if you can't load the data properly?<span id="more-139"></span></p>
<h1>Solution 1: Use a server-side proxy</h1>
<p>The first way around this problem is to use a very simple server-side script that acts as a proxy to the web service. So instead of requesting AJAX from the remote site, you request it locally on your own domain through the proxy. The proxy itself has it's programming logic to send your request off to the remote site, gather the data, and then serve it back to you.</p>
<pre>.________________________.
| Client on YourSite.com |
'-\/---------/\----------'
  ||         ||
  ||_________||__.
  | AJAX Request |
  '---\/-----/\--'
      ||     ||
      ||_____||______________.
      | YourSite.com         |
      | (Ex. ajax_proxy.php) |
      '---\/-----/\----------'
          ||     ||
          ||_____||__________.
          | OtherSite.com    |
          '------------------'</pre>
<p>Since your server-side script has no qualms about fetching data from another domain, you can successfully proxy all AJAX like this without running into any trouble. And then since the client browser is fetching the data from the local domain (even though you're making another request behind the scenes), it doesn't violate the same origin policy.</p>
<p>An example proxy script might look something like this (maybe a little more complex if you're handling POST data too):</p>
<pre id="raw-php-10" style="display:none; width: 1px; height: 1px; overflow: hidden;">$url = 'http://othersite.com/someservice?' . http_build_query($_GET);
echo file_get_contents($url);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-10" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-10'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$url</span> = <span style="color:#FF0000;">'http://othersite.com/someservice?'</span> . http_build_query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <a href="http://www.php.net/file_get_contents"><span style="color:#000066;">file_get_contents</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
Remember, the sole purpose of the proxy is just so the client browser can load the data locally on the same domain so the same origin policy isn't violated.</p>
<h2>Drawbacks</h2>
<p>This method works well but has two obvious drawbacks. First is of course that you need a server-side script at all. Especially if you are providing a service, this raises the barrier for entry and makes it harder for "noobs" to use your widget -- it's not a simple "paste this code into your footer" instruction; you also need to explain how to install the proxy service.</p>
<p>The second drawback is that your own servers are making these requests which makes the whole process slower, but also eats up your resources -- both your processing power and even just simple bandwidth.</p>
<h1>Solution 2: JSONP</h1>
<p>The second solution is to use JSONP -- "JSON with Padding." This is a technique that lets you get around the same origin policy. In order to use JSONP, the service you are requesting needs to support it.</p>
<p>So in the last solution, the consumer (the user using the service) required an ajax_proxy.php or whatever server-side proxy script. In this solution, the provider (Digg, Amazon, Yahoo -- whatever) needs to support JSONP themselves. Thankfully, these days the idea is catching on and it's likely JSONP is an option. And if you're a service provider, then you'll want to build it in to your service for sure.</p>
<h2>How it works</h2>
<p>A normal JSON request is sent using XMLHttpRequest and the reply looks something like this:</p>
<pre id="raw-javascript-11" style="display:none; width: 1px; height: 1px; overflow: hidden;">{'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-11" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-11'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Now the truth is, JSONP isn't AJAX at all technically speaking because it does <em>not</em> use XMLHttpRequest. JSONP requests are made by dynamically inserting a &lt;script&gt; tag into the DOM. For example, you'd insert this:</p>
<pre id="raw-html-12" style="display:none; width: 1px; height: 1px; overflow: hidden;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://othersite.com/service?all=your&amp;params=as&amp;usual=gohere&quot;&gt;&lt;/script&gt;</pre>
<div class="igBar">
<div class="wrap"><span id="lhtml-12" style="float:right"><a href="#" onclick="javascript:showCodeTxt('html-12'); return false;">Plain Text</a></span><span class="langName">HTML:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="html-12">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"http://othersite.com/service?all=your&amp;params=as&amp;usual=gohere"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Then that remote Javascript file is loaded and contains an actual Javascript function call. For example:</p>
<pre id="raw-javascript-13" style="display:none; width: 1px; height: 1px; overflow: hidden;">handleJsonReply({'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-13" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-13'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-13">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">handleJsonReply<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>In other words, instead of reading AJAX data directly into a variable in Javascript, you define a callback function that is called when the data arrives, and the first parameter is the data itself as an object literal. If you were a provider implementing JSON on your service you might have something like:</p>
<pre id="raw-php-14" style="display:none; width: 1px; height: 1px; overflow: hidden;">// A service provider accepts a 'callback' parameter
// that it uses to wrap an AJAX reply

// Imagine we did some work here
$json = json_encode($mydata);

// Using JSONP
if ($_GET['callback']) {
	echo $_GET['callback'] . &quot;($json);&quot;; // somefunction({data here});

// Normal JSON
} else {
	echo $json;
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-14" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-14'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// A service provider accepts a 'callback' parameter</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// that it uses to wrap an AJAX reply</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Imagine we did some work here</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$json</span> = json_encode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$mydata</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Using JSONP</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'callback'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'callback'</span><span style="color:#006600; font-weight:bold;">&#93;</span> . <span style="color:#FF0000;">"($json);"</span>; <span style="color:#FF9933; font-style:italic;">// somefunction({data here});</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Normal JSON</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$json</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The Javascript source code result of that request is nothing but a real executable function call with a Javascript object literal. It's as if you added the tag yourself and it called some functions -- the difference being that the JS is written by the producer on-the-fly and embeds the data you want right in the code. This is why it all works; it's not actually AJAX at all, it's just loading a remote Javascript file that happens to have some useful data in it.</p>
<p>Using a library like jQuery that supports JSONP, these details of inserting the special script tag and creating the special callback function are all taken care of automatically. Using a JS library, usually the only difference between JSONP and real AJAX is that you enable a 'jsonp' option.</p>
<h2>Drawbacks</h2>
<p>The first limitation to this method is that you have to rely on the provider to implement JSONP. The provider needs to actually support JSONP -- they need to wrap their JSON data with that callback function name.</p>
<p>Then the next limitation -- and this is a big one -- is that JSONP doesn't support POST requests. Since all data is passed in the query string as GET data, you are severely limited if your services require the passing of long data (for example, forum posts or comments or articles). But for the majority of consumer services that <em>fetch</em> more data than they push, this isn't such a big problem.</p>
<h2>JSONP with jQuery</h2>
<p>Step 1: Make sure the provider supports JSONP.<br />
Step 2: Set the <code>dataType</code> option to <code>jsonp</code>, and if the provider uses a different GET param other than 'callback', specify the <code>jsonp</code> option to that parameter name.</p>
<pre id="raw-javascript-15" style="display:none; width: 1px; height: 1px; overflow: hidden;">$.ajax({
    // ... Use the AJAX utility as you normally would
    dataType: 'jsonp',
    // ...
});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-15" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-15'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-15">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$.<span style="color: #006600;">ajax</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// ... Use the AJAX utility as you normally would</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; dataType: <span style="color: #3366CC;">'jsonp'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>jQuery will generate a unique callback name for this request (something like json1268267816). Thus, the reply from a web service would be something like:</p>
<pre id="raw-javascript-16" style="display:none; width: 1px; height: 1px; overflow: hidden;">json1268267816({'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-16" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-16'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-16">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">json1268267816<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
But jQuery handles it all seamlessly, so you as the developer just handle it like a normal AJAX request using the same jQuery success/failure/complete callback hooks.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2010/03/10/cross-domain-ajax/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Creating daemons in PHP</title>
		<link>http://devlog.info/2010/03/07/creating-daemons-in-php/</link>
		<comments>http://devlog.info/2010/03/07/creating-daemons-in-php/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 21:42:26 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[daemon]]></category>
		<category><![CDATA[supervisord]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=126</guid>
		<description><![CDATA[Today I want to talk about daemonizing PHP scripts on Linux servers -- creating scripts that are meant to run in the background for long periods of time. After we've covered that, I'll explain how I like to use supervisord to manage these long-running PHP processes.

Creating long-running scripts
A daemon is nothing but a script that [...]]]></description>
			<content:encoded><![CDATA[<p>Today I want to talk about daemonizing PHP scripts on Linux servers -- creating scripts that are meant to run in the background for long periods of time. After we've covered that, I'll explain how I like to use <a href="http://supervisord.org/">supervisord</a> to manage these long-running PHP processes.<br />
<span id="more-126"></span></p>
<h2>Creating long-running scripts</h2>
<p>A daemon is nothing but a script that goes on forever. When you get right down to it, a daemon is simply an infinite loop.</p>
<pre id="raw-php-23" style="display:none; width: 1px; height: 1px; overflow: hidden;">class MyDaemon
{
	public function run()
	{
		while (1) {
			// Like the energizer bunny
		}
	}
}

$daemon = new MyDaemon();
$daemon-&gt;run();</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-23" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-23'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-23">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MyDaemon</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Like the energizer bunny</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$daemon</span> = <span style="color:#000000; font-weight:bold;">new</span> MyDaemon<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$daemon</span>-&gt;<span style="color:#006600;">run</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The work you do within that infinite loop is the whole purpose of your daemon. Maybe it's listening for a network connection, or maybe it's waiting for an item to enter a queue. For example, how about a hypothetical website that needs to process new users using a database queue, you might end up with something like this:</p>
<p><em>(Note: using a database for a queue isn't very efficient, hence the use of sleep() here to prevent hundreds of count queries per second being run in the loop. In practice you'd want to use a real queue, like <a href="http://kr.github.com/beanstalkd/">beanstalkd</a>.)</em></p>
<pre id="raw-php-24" style="display:none; width: 1px; height: 1px; overflow: hidden;">class MyDaemon
{
	public function run()
	{
		while (1) {
			if ($this-&gt;isNewUsers()) {
				foreach ($this-&gt;getNewUsers() as $user) {
					$this-&gt;setupNewUser($user);
				}
			} else {
				// If there's no work to do, let's just
				// wait 10 seconds before hitting the DB to
				// count again
				sleep(10);
			}
		}
	}

	// ...
	// Imagine we defined isNewUsers, getNewUsers, setupNewUser
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-24" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-24'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-24">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MyDaemon</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">isNewUsers</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">getNewUsers</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$user</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">setupNewUser</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$user</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// If there's no work to do, let's just</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// wait 10 seconds before hitting the DB to</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// count again</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/sleep"><span style="color:#000066;">sleep</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Imagine we defined isNewUsers, getNewUsers, setupNewUser</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p><em>Note: In this post I'm creating simple daemons that generally perform one task at a time. "True" daemons, like a web server for example, fork off children so multiple tasks can be run in parallel.</em></p>
<h2>Shutting down safely</h2>
<p>So we have a long-running script now. But you might have noticed there's no way to actually stop it. The only way we could ever stop this script from running is to issue a <em>kill</em> command to kill the process. But this is dangerous because what if the script was in the middle of processing something? Using our example above, what if we were only half-finished processing a user?</p>
<p>You can sometimes mitigate problems like this. For example, if you use database transactions to ensure an all-or-nothing commit. But what if you need to interact with systems that don't offer such transactional safety?</p>
<p>So the only way a PHP daemon is really worth it is if we can shut it down safely. That means we need some way to tell the script it's time to go; a way to tell it to <code>break</code> the infinite loop. Something like this:</p>
<pre id="raw-php-25" style="display:none; width: 1px; height: 1px; overflow: hidden;">class MyDaemon
{
	public function run()
	{
		while (1) {
			// Check to see if we should break out
			if ($this-&gt;shouldStop()) break;

			if ($this-&gt;isNewUsers()) {
				foreach ($this-&gt;getNewUsers() as $user) {

					// Check inside the loop too, to make sure we
					// dont continue processing a big batch of users
					if ($this-&gt;shouldStop()) break;

					$this-&gt;setupNewUser($user);
				}
			} else {
				sleep(10);
			}
		}
	}

	/**
	 * Returns true when the main loop should finally stop
	 */
	public function shouldStop()
	{
		return false;
	}
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-25" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-25'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-25">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MyDaemon</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Check to see if we should break out</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">shouldStop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">isNewUsers</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">getNewUsers</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$user</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Check inside the loop too, to make sure we</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// dont continue processing a big batch of users</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">shouldStop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">setupNewUser</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$user</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/sleep"><span style="color:#000066;">sleep</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">/**</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">&nbsp; &nbsp;&nbsp; * Returns true when the main loop should finally stop</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">&nbsp; &nbsp;&nbsp; */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> shouldStop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>But what can we put inside that <code>shouldStop()</code> method?</p>
<h3>Checking an external value</h3>
<p>Perhaps the simplest thing to do is to check to see if there is some external value. Maybe a database record that says "should_stop=1" or the presence of a file called "should_stop.txt".</p>
<pre id="raw-php-26" style="display:none; width: 1px; height: 1px; overflow: hidden;">	/**
	 * Returns true when the main loop should finally stop
	 */
	public function shouldStop()
	{
		clearstatcache();
		if (file_exists('should_stop.txt')) {
			unlink('should_stop.txt'); // delete the file for next time
			return true;
		}

		return false;
	}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-26" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-26'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-26">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">&nbsp; &nbsp;&nbsp; * Returns true when the main loop should finally stop</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">&nbsp; &nbsp;&nbsp; */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> shouldStop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/clearstatcache"><span style="color:#000066;">clearstatcache</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'should_stop.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/unlink"><span style="color:#000066;">unlink</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'should_stop.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// delete the file for next time</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>But these methods aren't very efficient, and they build up a dependance on some other system. </p>
<h2>Handling POSIX signals</h2>
<p>The best way for us to handle clean shutdowns is to handle standard POSIX signals like <code>TERM</code> and <code>INT</code>. When you're using a shell and press CTRL+C, Linux sends the application an <code>INT</code> signal. When you the <em>kill</em> command on a process, it will send the <code>TERM</code> signal by default. So intercepting these two signals is perfect for us. If we can intercept them, then we can make sure we shut down gracefully.</p>
<h3>Enabling Process Control in PHP</h3>
<p>PHP has <a href="http://www.php.net/manual/en/book.pcntl.php">process control features (PCNTL)</a> that we need to interpret POSIX signals. But they are not enabled by default. You must compile PHP with the <code>--enable-pcntl</code> configuration option.</p>
<h3>Listening for signals</h3>
<p>There are two steps we need to do:</p>
<ol>
<li><a href="http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks">Declare ticks</a>. Ticks are a little known feature of PHP that allows you to run a callback function every <em>X</em> statements. This can be useful for memory profiling or debugging, but it's not very useful otherwise. But we aren't actually going to use ticks ourselves -- the PHP PCNTL features just uses it internally.</li>
<li>Assign callbacks to listen for signals using <code><a href="http://www.php.net/manual/en/function.pcntl-signal.php">pcntl_signal()</a></code>.</li>
</ol>
<p>Here's what we end up with:</p>
<pre id="raw-php-27" style="display:none; width: 1px; height: 1px; overflow: hidden;">declare(ticks = 1);

function sig_handler($signo)
{
	switch ($signo) {
		case SIGTERM:
		case SIGINT:
			global $_MYDAEMON_SHOULD_STOP;
			$_MYDAEMON_SHOULD_STOP = true;
			break;
	}
}
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');

class MyDaemon
{
	// ...

	public function shouldStop()
	{
		global $_MYDAEMON_SHOULD_STOP;
		if (isset($_MYDAEMON_SHOULD_STOP) AND $_MYDAEMON_SHOULD_STOP) {
			return true;
		}

		return false;
	}
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-27" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-27'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-27">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks = <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">switch</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">case</span> SIGTERM:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">case</span> SIGINT:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$_MYDAEMON_SHOULD_STOP</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$_MYDAEMON_SHOULD_STOP</span> = <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span>SIGTERM, <span style="color:#FF0000;">'sig_handler'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span>SIGINT, <span style="color:#FF0000;">'sig_handler'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">class</span> MyDaemon</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> shouldStop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$_MYDAEMON_SHOULD_STOP</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_MYDAEMON_SHOULD_STOP</span><span style="color:#006600; font-weight:bold;">&#41;</span> AND <span style="color:#0000FF;">$_MYDAEMON_SHOULD_STOP</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>So in this example we've created a callback function that listens for <code>TERM</code> and <code>INT</code> signals, and assigns a value of 'true' to a variable. Then within the actual daemon class code, we check this variable each time to determine when it's time to call it quits.</p>
<p>Note that it's important to use a basic function callback with <code>pcntl_signal</code> and not an object method. There is some weirdness when using using <code>$this</code> in callbacks. That's why in the example above I've opted to use a simple function and a global variable.</p>
<h2>Problems with long-running PHP scripts</h2>
<p>The biggest problem with long-running PHP scripts is memory usage. PHP isn't exactly designed for long-running programs. There are memory leaks that don't usually matter in websites because requests start and finish so quickly, but matter a lot in long-running processes. These leaks are usually to do with circular object references and PHP's garbage collection. But with recent versions of PHP it's not so much of a problem anymore.</p>
<p>Even so, you have to be careful about memory usage in your actual scripts as well. Not many libraries are designed for long-running processes. So maybe your emailing library saves some information in an array after you send each email. In a web script, it doesn't  matter -- the array is lost after the request finishes. But now you might have a process that runs for hours on end. This little array of log messages can become huge!</p>
<p>It's problems like that that you must be weary of. You need to consciously think about things like that while designing your PHP daemons. What I would recommend is to plan for your PHP daemons, if possible, to shut down and restart periodically. I'll explain the best way to do this in the next section.</p>
<h2>Handling processes (startup, monitoring, detaching etc)</h2>
<p>So we've got our long-running PHP scripts now, but we still have a few more things to tackle. First is how do we detach a script so it runs in the background? That is, if you run one of these scripts from your shell for example, you'll just get an empty screen that does nothing until you quit the process. You want to run the script so it <em>detaches</em> and runs in the background -- aka, you get your command prompt back. You <em>can</em> do this in straight PHP -- it involves forking so the parent process can die, but the children live on.</p>
<p>But there are other things to consider. How do we handle output? How do we handle fatal errors and exceptions (like a database disconnection) -- how do we get the process to restart? How do we make sure the correct user owns the process?</p>
<p>The easiest way to do all of this is to use a process manager like <a href="http://supervisord.org/">supervisord</a>. This software runs on your server and is responsible for starting your long-running PHP scripts in the background (so you don't have to worry about detaching yourself), and also sports features like monitoring so you can automatically restart processes that've quit unexpectedly or when they are using too much memory.</p>
<p>With supervisord, you add some configuration for each script you want to run. Here's an example that runs a script, logs output to a file, and will TERM the script when it uses over 50MB of memory (where it'll be restarted again).</p>
<pre id="raw-code-28" style="display:none; width: 1px; height: 1px; overflow: hidden;">[program:mydaemon]
command=/usr/bin/php /home/john/mydaemon.php
directory=/home/john
startsecs=5
exitcodes=2
user=john
stdout_logfile=/home/john/logs/mydaemon.out
stderr_logfile=/home/john/logs/mydaemon.err

[eventlistener:memmon]
command=memmon -p mydaemon=50MB
events=TICK_60</pre>
<div class="igBar">
<div class="wrap"><span id="lcode-28" style="float:right"><a href="#" onclick="javascript:showCodeTxt('code-28'); return false;">Plain Text</a></span><span class="langName">CODE:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="code-28">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>program:mydaemon<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">command=/usr/bin/php /home/john/mydaemon.<span style="">php</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">directory=/home/john</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">startsecs=<span style="color:#800000;color:#800000;">5</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">exitcodes=<span style="color:#800000;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user=john</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stdout_logfile=/home/john/logs/mydaemon.<span style="">out</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stderr_logfile=/home/john/logs/mydaemon.<span style="">err</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>eventlistener:memmon<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">command=memmon -p mydaemon=50MB</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">events=TICK_60 </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>There are other good features of supervisord and other configuration options -- be sure to check it out, it's insanely useful.</p>
<h2>That's all, folks</h2>
<p>So that's how I write long-running PHP scripts. Don't fuss around with minutely cron jobs to run PHP scripts anymore. Do it properly. Create a daemon.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2010/03/07/creating-daemons-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Why PHP&#8217;s $_REQUEST is dangerous</title>
		<link>http://devlog.info/2010/02/04/why-php-request-array-is-dangerous/</link>
		<comments>http://devlog.info/2010/02/04/why-php-request-array-is-dangerous/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 07:54:37 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=113</guid>
		<description><![CDATA[PHP offers a convenience superglobal called $_REQUEST that coalesces input from a number of sources into one easy to use array. A common thought amongst PHP developers is that you should avoid the use of $_REQUEST -- but do you actually know why?
The prevailing misconception seems to be that having GET and POST variables mixed [...]]]></description>
			<content:encoded><![CDATA[<p>PHP offers a convenience superglobal called <a href="http://php.net/manual/en/reserved.variables.request.php">$_REQUEST</a> that coalesces input from a number of sources into one easy to use array. A common thought amongst PHP developers is that you should avoid the use of $_REQUEST -- but do you actually know why?<span id="more-113"></span></p>
<p>The prevailing misconception seems to be that having GET and POST variables mixed together is insecure. While this is true under some specific circumstances (see <em><a href="http://devlog.info/2007/09/02/cross-site-request-forgeries-csrf/">Cross-Site Request Forgeries</a></em> for a weak example) -- it isn't the main reason why $_REQUEST is insecure. In fact, combining GET and POST data is often a very useful feature. In many cases you don't really care if a variable comes from GET or POST and so using $_REQUEST in those situations can help reduce the verbosity of your code while increasing flexibility.</p>
<p>The real problem is that <em>COOKIE data</em> is also mixed into $_REQUEST. The implications of this may not be readily apparent until you start thinking like a malicious user. Any cookie ever set on a users machine will always override any GET or POST data -- creating "sticky" variable data.</p>
<p>For instance, imagine if your application uses "action=" or "do=" switches to control page commands. If an attacker some how managed to set a cookie -- perhaps through some Javascript injection -- he could set a cookie named "action" and give it the value "logout." Any users that fell victim to the cookie set would never be able to use your application because "action" would be permanently set to "logout."</p>
<p>So to sum it up:</p>
<p><strong>Question:</strong> Why is $_REQUEST insecure?<br />
<strong>Answer:</strong> Because it combines COOKIE as well as GET and POST, and the COOKIE data always takes precedence -- creating the possibility for dangerous "sticky" variables.</p>
<h4>Solutions</h4>
<p>If you're already using PHP 5.3, then you can edit your php.ini and check the <a href="http://php.net/manual/en/ini.core.php#ini.request-order">request_order</a> variable -- make sure 'C' removed. <em>This 'C' is removed by default</em>, but if you use a shared host it might've been put back for backwards-compatibility. Make sure to check!</p>
<p>Otherwise, you can easily just create your own $_REQUEST array by merging $_GET and $_POST manually as part of your global initialization routines.</p>
<pre id="raw-php-30" style="display:none; width: 1px; height: 1px; overflow: hidden;">$_REQUEST = array_merge($_GET, $_POST);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-30" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-30'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-30">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$_REQUEST</span> = <a href="http://www.php.net/array_merge"><span style="color:#000066;">array_merge</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span>, <span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>And finally, best practice is to abstract your input reading out (see <em><a href="http://devlog.info/2009/02/07/stop-using-superglobals/">Stop using superglobals!</a></em>) so you can define yourself exactly how variables are read and from where.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2010/02/04/why-php-request-array-is-dangerous/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>8 Features of Badly Designed Software</title>
		<link>http://devlog.info/2009/09/16/8-features-of-badly-designed-software/</link>
		<comments>http://devlog.info/2009/09/16/8-features-of-badly-designed-software/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 02:18:05 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Application Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=89</guid>
		<description><![CDATA[I've compiled a list of 8 features that are inherent to all badly designed software. This list is in regards to code design -- it's about architecture.
1. High Coupling
All software you write depends on some other software. And parts of your own software depends on other parts of your software. Coupling is how much one [...]]]></description>
			<content:encoded><![CDATA[<p>I've compiled a list of 8 features that are inherent to all badly designed software. This list is in regards to code design -- it's about architecture.<span id="more-89"></span></p>
<h2>1. High Coupling</h2>
<p>All software you write depends on some other software. And parts of your own software depends on other parts of your software. Coupling is <em>how much</em> one part of your software depends on another part. In a highly coupled system, lots of parts all depend on each other. In a lowly coupled system, parts are independent from each other as much as possible.</p>
<h3>Why It's Bad</h3>
<ul>
<li>Changes made to one part of your system affect other parts of your system. This means making changes becomes difficult and error prone.</li>
<li>It becomes difficult or impossible to use even functionally unrelated modules separately because they depend on each other so heavily.</li>
<li>Due to the previous point, it is nearly impossible to write unit tests. This compounds the problem of making changes because you can't properly test to see if your changes break things in other parts of your system.</li>
</ul>
<h3>Solution</h3>
<p>Stop coupling! Write classes that are as separate from each other as you can. This may mean building utility classes, or classes that help "connect" bits of your system. The point is that Part A of your system should be completely separate from Part B of your system.</p>
<h3>Further Reading</h3>
<ul>
<li><a href="http://www.codeodor.com/index.cfm/2009/6/17/Strive-for-low-coupling-and-high-cohesion-What-does-that-even-mean/2902">Strive for low coupling and high cohesion: What does that even mean?</a>
</li>
</ul>
<h2>2. Premature Optimization</h2>
<p>We've all heard the famous quote by Donald Knuth, the author of The Art of Computer Programming: <em>Premature optimization is the root of all evil.</em></p>
<p>Developers often worry about the performance of their software before performance should be considered at all.</p>
<h3>Why It's Bad</h3>
<ul>
<li>A developer who writes code with performance as a first priority will produce code that is hard to use and maintain.</li>
<li>Code that is "optimized" prematurely rarely ever results in software that actually runs faster. Without proper testing/benchmarking, it is often hard to know which parts of your system is the bottleneck. Saving 15 milliseconds on a calculation routine does nothing for the data-fetching routine that takes 1500 milliseconds.</li>
<li>Due to the lack of proper testing, developers will spend too much time on menial performance optimizations while missing the important ones.</li>
</ul>
<h3>Solution</h3>
<p>Write your code with performance in your mind -- but don't sacrifice usability and maintainability. Write your code first, and then only once you have identified real bottlenecks you can start optimizing.</p>
<h2>3. Abusing Inheritance </h2>
<p>Inheritance is great, but some developers try to force their software to fit into rigid inheritance trees. Inheritance is a tool, but it's not the only way to write reusable software.</p>
<h3>Why It's Bad</h3>
<ul>
<li>Abusing inheritance results in classes that have functionality that they shouldn't.</li>
<li>Subclasses make assumptions about their parent classes. This couples children to their parents. As we know, coupling is bad. The deeper our class tree gets, the more coupling, and the worse the design is.</li>
<li>You end up with huge families of classes. For example: A parent Animal class. Then a Dog child. Then a BlackDog, and a BlackBigDog, and BlackSmallDog, and BlackBigLoudDog and BlackSmallLoudDog... You can see how it quickly gets out of control!</li>
<li>You tie functionality/algorithms to a specific family of classes, making it harder to reuse. The only way to access such functionality is through inheritance -- even when inheritance doesn't exactly fit the bill.</li>
</ul>
<h3>Solution</h3>
<p>The old maxim: <em>Favor composition over inheritance</em>.</p>
<p>Composition <em>adds</em> functionality at runtime -- giving you much flexibility. Inheritance forces you to define all functionality at code-time, limiting possibilities.</p>
<p>Composition means your classes are built up (composed) of other classes for additional functionality. This reduces needless children, and decouples functionality into separate reusable classes of their own. Using the example mentioned previously, a Dog class might be made up of separate classes to define it's appearance (AnimalSize class?) and loudness of it's bark (AnimalSound class?). An example calls might then be, <var>$dog->getSize()->display()</var> and <var>$dog->getSound()->makeSound()</var>.</p>
<p>In other words, you want "HAS A" (composition) relationships rather than "IS A" (inheritance) relationships.</p>
<h2>4. Failure to separate responsibilities</h2>
<p>Some developers fall into the trap of creating classes that do way too much -- so called God Objects. Classes should do one thing only (see the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single responsibility principle</a>).</p>
<h3>Why It's Bad</h3>
<ul>
<li>Other parts of your system depend too much on a single class, creating very high coupling. Any changes made will ripple out to all it's dependants.</li>
<li>It is hard to reuse code written in these classes because they come with so much "baggage". Instantiating such a monster object wastes resources with all of the unneeded functionality that has been added.</li>
</ul>
<h3>Solution</h3>
<p>Refactor. The God Object needs to be properly deconstructed into smaller, more specialized classes that all do a single job.</p>
<h2>5. Failure to refactor</h2>
<p>Refactoring is the activity of switching around your softwares internal design, while keeping it's functionality the same. For example, renaming a method is a small example of refactoring. A more intense example might be splitting one class into two separate classes (maybe you're tearing apart a God Object?).</p>
<h3>Why It's Bad</h3>
<ul>
<li>As your product grows older and you add new features, classes will start to gain functionality they did not before. Over time, classes may become too big resulting in monster or God Objects.</li>
<li>As your product grows and you add features, some classes may start to become complex. Without refactoring to simplify the inner workings of your system, this complexity may start to get out of control. For example, a class constructor that takes 20 arguments.</li>
<li>The longer refactoring is put off, the worse the problem becomes and the harder the task will be in the future.</li>
</ul>
<h3>Solution</h3>
<p>Refactor as soon as you encounter <a href="http://en.wikipedia.org/wiki/Code_smell">smelly code</a>. The earlier you refactor, the easier it will be.</p>
<h2>6. Complexity</h2>
<p>Code should be simple to read and maintain. Functions that take 20 arguments, or classes that require too many helper objects are examples of complex designs that can be fixed.</p>
<h3>Why It's Bad</h3>
<ul>
<li>Complex designs are hard to understand and thus hard to maintain.</li>
<li>Complex designs will result in hard to find bugs.</li>
</ul>
<h3>Solutions</h3>
<p>Simplify. If a function takes 20 arguments, maybe some of those arguments can be encapsulated into a class. Or maybe the one function can be replaced by an easier to use class. If a class requires lots of setup, consider using the <a href="http://en.wikipedia.org/wiki/Builder_pattern">Builder pattern</a>. If you have a system of classes that all work together towards some goal, consider simplifying the interface by using a <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facade</a>.</p>
<h2>7. Reinventing The Wheel</h2>
<p>Some developers, especially amateurs, like to create everything from scratch and ignore all of the available free library code that exists on the internet.</p>
<h3>Why It's Bad</h3>
<ul>
<li>You waste time writing components when you could be actually building your product.</li>
<li>You get none of the free testing done against a popular library. Anything you write from scratch will contain bugs.</li>
<li>Not always true (but may especially apply to amateurs): Many libraries are written by developers who may be more experienced than you. These coders produce better organized code with more features.</li>
<li>Not always true: Developers of libraries are often experts in whatever task the library is meant to solve. For example, database abstraction. Because of this, the library developers will produce a better product than you could.</li>
</ul>
<h3>Solutions</h3>
<p>Realize that writing everything in-house is almost always a bad thing. There will of course be times where there exists no suitable library. But almost everything has been written before, and written well. A good developer knows the tools at his disposal, and uses them.</p>
<p>Every time you consider writing something yourself, you should search for a good library first. Common difficult tasks such as database abstraction, web services, image manipulation, validation and parsing are examples of components where an abundance of free library code exists. Less well-known problems, or new problems, may not have many pre-built solutions.</p>
<h2>8. Failure to apply known design patterns</h2>
<p><a href="http://en.wikipedia.org/wiki/Design_patterns">Design patterns</a> are well known solutions to architectural problems in software. I've mentioned a couple in this article already (Builder, Facade).</p>
<h3>Why It's bad</h3>
<ul>
<li>A developer who doesn't know design patterns is more likely to design his software incorrectly.</li>
<li>Not knowing about design patterns will decrease your efficiency. Every problem has been solved before. By knowing which pattern to apply to a problem, half of your work is done for you. Otherwise, you will go through many revisions before you end up at the same solution anyway.</li>
<li>Design patterns help a developer communicate with other developers. It's the language of the profession. You will not be able to convey ideas effectively if you don't understand the terminology.</a>
</ul>
<h3>Solution</h3>
<p>Read all material you can find on the topic of design patterns (see next section). Amateur programmers may not appreciate the value of these patterns at first -- or, indeed, may not fully understand them -- but as time goes on, this knowledge is priceless.</p>
<h3>Further Reading</h3>
<ul>
<li><a href="http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124">Head First Design Patterns</a></li>
<li><a href="http://www.amazon.ca/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612"> Design Patterns: Elements of Reusable Object-Oriented Software</a></li>
<li><a href="http://www.amazon.ca/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420"> Patterns of Enterprise Application Architecture</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2009/09/16/8-features-of-badly-designed-software/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>About Timestamps</title>
		<link>http://devlog.info/2009/02/11/about-timestamps/</link>
		<comments>http://devlog.info/2009/02/11/about-timestamps/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 01:50:52 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=80</guid>
		<description><![CDATA[A friend of mine who is rather new to PHP was getting super confused about timestamps and timezones. So I thought I'd post a quick thought about timestamps today to clear up some confusion.
As you know, timestamps represent the number of seconds elapsed since January 1, 1970 00:00:00 UTC. The UTC timezone there is important. [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine who is rather new to PHP was getting super confused about timestamps and timezones. So I thought I'd post a quick thought about timestamps today to clear up some confusion.<span id="more-80"></span></p>
<p>As you know, timestamps represent the number of seconds elapsed since January 1, 1970 00:00:00 UTC. The UTC timezone there is important. Basically all unix timestamps are in UTC -- there is no "EST timestamp" for example, there is only UTC.</p>
<p>Let's be clear about that: If we both compared our timestamps on our computers right now, regardless of where in the world we were, the timestamps would match. A timestamp is a timestamp is a timestamp (how's that for poetic geekery :-).</p>
<p>But confusion ensues when you want to <em>create</em> timestamps. For example, using PHP's <code>mktime()</code> function. The problem is that to specify a time you have to know what timezone it is. Imagine if I were to say, "It is 19:00". That statement would make no sense; it's ambiguous. To make any sense at all, I need to specify <em>where</em> it is 19:00 -- and our world does this via timezones.</p>
<p>The thing to keep in mind is that "human time" is different in every timezone. For humans, my 19:00 might be your 16:00, which might be someone else's 22:00. But in "computer time" of timestamps, all of these times are the same -- they are all timestamps. Because a point in time is the same point in time everywhere, only when we "render" time to "human time" do we introduce the concept of reference points (timezones).</p>
<p>How do I <em>make</em> a timestamp that is <em>my</em> 19:00? To do this, all I need to do is create a timestamp that is 19:00 UTC, and then calculate my timezone offset. Why does this work? Remember that 19:00 is the exact same point in time everywhere in the world -- the only thing that changes is how we humans reference that time. Thus, "my" 19:00 is not UTC 19:00. To get "my" 19:00, I get UTC's 19:00 and offset it for my timezone, so that the timestamp correctly represents 19:00 <em>from my point of view</em>.</p>
<h2>Timezones with PHP</h2>
<p>The point of this post was originally to talk about confusions with PHP. Because it seems a little known fact that PHP's <code>mktime()</code> converts input from local time into UTC. That is, it does the offset calculations for you.</p>
<p>So when I do this with PHP (create a time where it is 19:00):</p>
<pre id="raw-php-33" style="display:none; width: 1px; height: 1px; overflow: hidden;">mktime(19);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-33" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-33'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-33">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mktime"><span style="color:#000066;">mktime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">19</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
PHP uses your systems timezone and offsets the time for you. If the systems timezone is GMT-5, then it'll offset 5 hours to create the correct UTC representation of that time. (PHP will use the system defined timezone for your server unless you use <code><a href="http://php.net/date_default_timezone_set">date_default_timezone_set</a></code>)</p>
<p>What about when we do these:</p>
<pre id="raw-php-34" style="display:none; width: 1px; height: 1px; overflow: hidden;">time();
mktime();</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-34" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-34'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-34">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/time"><span style="color:#000066;">time</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mktime"><span style="color:#000066;">mktime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
These mean "the current timestamp". As we discussed before, a point in time is the exact same no matter where you are in the world -- so the timestamp for "right now" requires no offset calculations (there is no ambiguity for "right now"). Timezones only matter when you're <em>referencing a date and time specifically</em> (aka, using <code>mktime()</code> with arguments).</p>
<p>If you want to make a timestamp with time elements that are already in UTC time, then you should use the <code>gmmktime()</code> function instead. Alternatively, you could set PHP's internal timezone to UTC.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2009/02/11/about-timestamps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stop using superglobals!</title>
		<link>http://devlog.info/2009/02/07/stop-using-superglobals/</link>
		<comments>http://devlog.info/2009/02/07/stop-using-superglobals/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 21:47:45 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[input]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=70</guid>
		<description><![CDATA[Today I want to talk about best practices in regards to reading user input. And if you haven't already, I'm going to convince you why using PHP's superglobal arrays are bad.
Best Practice: Cast to what you want
The first part of this post is all about how important casting incoming data is. As you know, security [...]]]></description>
			<content:encoded><![CDATA[<p>Today I want to talk about best practices in regards to reading user input. And if you haven't already, I'm going to convince you why using PHP's superglobal arrays are bad.<span id="more-70"></span></p>
<h2>Best Practice: Cast to what you want</h2>
<p>The first part of this post is all about how important casting incoming data is. As you know, security rule number one is <em>never trust the user</em>. So if you are expecting an Article ID which is an integer, you should make sure that you actually got an integer.</p>
<p>The most common approch is to cast user input when you get it. So if an attacker tries to supply some arbitrary string, PHP will cast it to an integer which usually results in a harmless 0. Sure, you have precautions in place against SQL injection or XSS but by casting data to a specific type, you can greatly simplify code because you can start working under assumptions ("I know for sure that $id is a harmless integer").</p>
<p>Here are a few examples:</p>
<pre id="raw-php-42" style="display:none; width: 1px; height: 1px; overflow: hidden;">// ID's (the use of max to get rid of negatives; Usually ID's are unsigned)
$article_id = max(0, (int)$_GET['id']);

// Simple strings
$username = preg_replace('#^[a-zA-Z0-9\-_\.]$#', '', $_GET['show_user']);

// Booleans
$show_drafts = (bool)$_GET['show_drafts'];
</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-42" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-42'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-42">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ID's (the use of max to get rid of negatives; Usually ID's are unsigned)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <a href="http://www.php.net/max"><span style="color:#000066;">max</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">0</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>int<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Simple strings</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$username</span> = <a href="http://www.php.net/preg_replace"><span style="color:#000066;">preg_replace</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#^[a-zA-Z0-9<span style="color:#000099; font-weight:bold;">\-</span>_<span style="color:#000099; font-weight:bold;">\.</span>]$#'</span>, <span style="color:#FF0000;">''</span>, <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'show_user'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Booleans</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$show_drafts</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>bool<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'show_drafts'</span><span style="color:#006600; font-weight:bold;">&#93;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Note that this isn't really about form validation. If someone is filling out a form, you still need to make sure what they input is valid. For example, the "Simple strings" snippet above is useless to users when they're registering because you're not telling them that their input is invalid.</p>
<p>These strict casts are useful only when you don't expect users to fiddle with values. So things like <code>view.php?id=34</code> where the URL is generated by your app, or &lt;select&gt; values where the user shouldn't change values etc.</p>
<h2>Getting rid of the superglobals</h2>
<p>Well, sort of. We obviously can't completely stop using $_GET/$_POST/$_COOKIE for reading incoming data because there's no replacement. What I'm talking about is ridding your code of these superglobals -- pushing them into a special input class. A code snippet is worth a thousand words; here's what I'm talking about:</p>
<pre id="raw-php-43" style="display:none; width: 1px; height: 1px; overflow: hidden;">// Bad
$article_id = $_GET['id'];

// Better
$article_id = $input-&gt;get('id');</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-43" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-43'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-43">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Bad</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Better</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h2>Why</h2>
<p>The above snippet doesn't really show off anything other than the concept. So let's talk about what this mysterious input class is meant to do, and why it's better than using superglobals.</p>
<h3>Control</h3>
<p>You don't have much control over superglobals. They are simply arrays. You can't do anything special before trying to fetch values. You might be thinking about what kind of processing you would want to do. But think about this for a moment.</p>
<p>How many times have you done something like this:</p>
<pre id="raw-php-44" style="display:none; width: 1px; height: 1px; overflow: hidden;">$article_id = (int)$_GET['id'];

// Or
if (isset($_GET['id']) {
    $article_id = (int)$_GET['id'];
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-44" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-44'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-44">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>int<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Or</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$article_id</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>int<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Since you control the input class, you can add a bunch of features to make gathering and casting input really easy.</p>
<pre id="raw-php-45" style="display:none; width: 1px; height: 1px; overflow: hidden;">$article_id = $input-&gt;getInt('id');
$search_user = $input-&gt;getSimpleString('search_user');
$delete_ids = $input-&gt;getArrayOfInts('delete_ids');
$article_content = $input-&gt;getString('content');
</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-45" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-45'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-45">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getInt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$search_user</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getSimpleString</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'search_user'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$delete_ids</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getArrayOfInts</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'delete_ids'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_content</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getString</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'content'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h4>UTF-8 Handling</h4>
<p>One place where this sort of functionality is really useful is when you've switched to using UTF-8. <a href="http://devlog.info/2008/08/24/php-and-unicode-utf-8/">As you know</a>, it is possible for UTF-8 strings to be malformed -- and this is a security risk. So every time you read strings you must make sure they're valid. Without an input class, this would be a lot of work. With an input class, you can just modify your 'getString' method to add UTF-8 checking.</p>
<h4>Stripping magic quotes</h4>
<p>Another common task PHP programmers routinely need to do is handle <a href="http://php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc">magic quotes</a>. Most of us simply <a href="http://php.net/get_magic_quotes_gpc">test to see if magic quotes is enabled</a>, and run the entire superglobal array family through a function that strips the slashes -- basically undoing this devil-feature.</p>
<p>But by doing this, you're affecting the entire app (because, well, you're modifying a superglobal of course!). This might not matter to you, but it gets a bit dangerous if you use third-party libraries.</p>
<p>A friend of mine creates Facebook applications. The Facebook PHP library is clever enough to test for magic quotes and strips slashes out for values it needs. But what if you're a clever PHP programmer, and you stripped out the slashes as part of your "global.php" file? FB doesn't know that, so it just strips them again! This is an example of how modifying global data is a bad idea.</p>
<p>By moving input-reading into your own class, you can do whatever the hell you want to your data and can rest assured knowing you didn't affect anything else.</p>
<h3>Abstraction</h3>
<p>By creating this new class, you abstract the details of <em>how</em> input is gathered. You might be wondering why this is important -- isn't there just get/post/cookie? No! While the vast majority of your webpages will only use these types of input, there are certainly others. Here are two that jump to my mind.</p>
<h4>Command-Line</h4>
<p>If you make command-line scripts, getting arguments may be a perfect use for your input class.</p>
<h4>Friendly-URL's</h4>
<p>$_GET only works for values encoded in the query string. But if you are a creating an app with friendly URL's, values are sometimes embedded right into the URL. </p>
<p>For example: <code>/article/42/edit</code></p>
<p>This URL might mean to "edit" the article with ID of "42". Most frameworks, like Zend Framework's MVC components, make creating URL's like these very easy. In ZF, you define a <em>route</em> with placeholders of the values:</p>
<pre id="raw-php-46" style="display:none; width: 1px; height: 1px; overflow: hidden;">$router-&gt;addRoute('article_action', new Zend_Controller_Router_Route(
    'article/:article_id/:action',
    array('controller' =&gt; 'article', 'action' =&gt; 'view')
));</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-46" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-46'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-46">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$router</span>-&gt;<span style="color:#006600;">addRoute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'article_action'</span>, <span style="color:#000000; font-weight:bold;">new</span> Zend_Controller_Router_Route<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'article/:article_id/:action'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'controller'</span> =&gt; <span style="color:#FF0000;">'article'</span>, <span style="color:#FF0000;">'action'</span> =&gt; <span style="color:#FF0000;">'view'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The framework provides a way to get the value of 'article_id' and 'action' that you could plug right into your input class. It might look something like this:</p>
<pre id="raw-php-47" style="display:none; width: 1px; height: 1px; overflow: hidden;">$article_id = $input-&gt;getIntFromUrl('article_id');
$action = $input-&gt;getStringFromUrl('action');</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-47" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-47'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-47">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$article_id</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getIntFromUrl</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'article_id'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$action</span> = <span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getStringFromUrl</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'action'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h2>Building an input class</h2>
<p>Building your own input class is an excersice you can complete yourself. But I'll get you started.</p>
<p>First of all, what is our goal?</p>
<ul>
<li>Ability to clean data</li>
<li>Ability to get data from multiple sources</li>
</ul>
<p>I think a well-designed system calls for a handful of classes:</p>
<ul>
<li><code>Cleaner</code> takes values and cleans/casts them to a correct data type. We're using a separate class because it's not directly tied to input -- we might reuse this functionality elsewhere.</li>
<li><code>InputSource_*</code> are classes that read raw data from some source. We have one reader for each source. For example, <code>InputSource_Array</code> for reading information from an array (like supergloabls) or <code>InputReader_Url</code> for reading information from a friendly URL.
<li><code>InputReader</code> is the main class that ties the <code>Cleaner</code> and the <code>InputSource_*</code> classes together.
</ul>
<p>Here's how it might work:</p>
<pre id="raw-php-48" style="display:none; width: 1px; height: 1px; overflow: hidden;">$cleaner = new Cleaner();
$input = new InputReader($cleaner);
$input-&gt;addSource('req', new InputSource_Array($_REQUEST));
$input-&gt;addSource('get', new InputSource_Array($_GET));
$input-&gt;addSource('post', new InputSource_Array($_POST));
$input-&gt;addSource('cookie', new InputSource_Array($_COOKIE));
$input-&gt;setDefaultSource('req');

$input-&gt;getInt('id'); // Get from the 'req' source, a default
$input-&gt;getInt('id', 'get'); // Get from 'get' source

// $input-&gt;getInt() calls a corresponding $cleaner method to
// clean an integer.</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-48" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-48'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-48">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$cleaner</span> = <span style="color:#000000; font-weight:bold;">new</span> Cleaner<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span> = <span style="color:#000000; font-weight:bold;">new</span> InputReader<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$cleaner</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">addSource</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'req'</span>, <span style="color:#000000; font-weight:bold;">new</span> InputSource_Array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_REQUEST</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">addSource</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'get'</span>, <span style="color:#000000; font-weight:bold;">new</span> InputSource_Array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">addSource</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'post'</span>, <span style="color:#000000; font-weight:bold;">new</span> InputSource_Array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">addSource</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'cookie'</span>, <span style="color:#000000; font-weight:bold;">new</span> InputSource_Array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_COOKIE</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">setDefaultSource</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'req'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getInt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'id'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// Get from the 'req' source, a default</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$input</span>-&gt;<span style="color:#006600;">getInt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'id'</span>, <span style="color:#FF0000;">'get'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// Get from 'get' source</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// $input-&gt;getInt() calls a corresponding $cleaner method to</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// clean an integer. </span></div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2009/02/07/stop-using-superglobals/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Overview: Creating Scalable Websites</title>
		<link>http://devlog.info/2008/10/28/overview-creating-scalable-websites/</link>
		<comments>http://devlog.info/2008/10/28/overview-creating-scalable-websites/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 10:06:32 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Application Design]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[load balancing]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[scaling]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=62</guid>
		<description><![CDATA[Just a quick post today around the topic of scalability. This is really a broad topic, but I wanted to give sortof a "starting point" or overview of how to create very scalable websites. The newest of my projects requires the system to scale infinitely, so I had to do a lot of research. I [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post today around the topic of scalability. This is really a broad topic, but I wanted to give sortof a "starting point" or overview of how to create very scalable websites. The newest of my projects requires the system to scale infinitely, so I had to do a lot of research. I found enough articles on the subject but I never really found a checklist of sorts that pointed me in the right direction, everything was so open-ended.<span id="more-62"></span></p>
<h2>About Scalability</h2>
<p>A scalable system is a system that is easily maintainable and can easily grow to accommodate increased usage.</p>
<p>Scalability is <em>not</em> about performance. Though you should aim towards a good performing system, performance and scalability are completely separate topics. Scalability is about maintaining the same level of performance as the usage grows.</p>
<p>Scalability is not about specific technologies (PHP, Ruby, Python, .NET, Java etc). Any language can be used to create a scalable system. In the realm of web applications, a website that is written in a scripting language like Python can be just as fast and scalable as one written in C. Large parts of Yahoo! for example are written in PHP -- and we know that scales. The language makes no difference because the speed of execution is not the trouble with scalable systems, it's the data. We'll come back to this idea in a bit.</p>
<h3>Vertical Scaling or "scaling up"</h3>
<p>Vertical scaling is achieving better performance by simply adding <em>better</em> hardware. If your system runs well with 1000 users but not so well at 5000 users, then you can simply upgrade your server with the fastest hardware and you're set.</p>
<p>Vertical scaling is easy. You don't need to think about scaling at all when writing your applications because it's essentially always running in one environment, on one server. If things get too busy, you just upgrade your server.</p>
<p>The problem with scaling up is that it is expensive. Low-end hardware is very cheap, but high end hardware is exponentially more expensive. There comes a time where the cost of the hardware is simply too much, or, that the hardware with the required resources simply does not exist.</p>
<p>So, vertical scaling is a type of scaling that you usually get "for free" without any work. But it is not practical if you want to build a site that serves millions.</p>
<p>Summary:</p>
<ul>
<li>Better hardware = better performance</li>
<li>Pros: Easy</li>
<li>Cons: Expensive; there will always be a technological limit to how powerful a machine you can possibly buy.</li>
</ul>
<h3>Horizontal Scaling or "scaling out"</h3>
<p>Scaling out is achieving better performance by adding <em>more</em> servers. Instead of constantly upgrading to more powerful machines, we can buy a bunch of less-powerful machines cheaply. It is much cheaper to buy many commodity servers than it is to buy one really powerful server. And unlike vertical scaling which has a "limit" to how powerful of a machine you can possibly buy, horizontal scaling can go on forever.</p>
<p>Basically instead of 1 really powerful machine with 32GB of memory and 4 processors, you have 8 commodity machines with 1 processor and 4GB of memory. Each machine handles a portion of the load instead of one machine handling it all. When you need more resources, it's as simple as adding a new (and cheap) commodity machine.</p>
<p>The cost of scaling out comes in two new flavors. The first is the cost of administering these new machines. Instead of say 1 monster server you now have to babysit 8 smaller servers. The real-world cost of doing this though is (fortunately) relatively low. Each machine you add to your system should be identical, and so, system administration tasks can be largely automated.</p>
<p>The second cost comes in design difficulty. You have to think a lot more while designing your system to make sure it <em>can</em> scale out. That is, how does your system work accross 8 separate servers?</p>
<p>Summary:</p>
<ul>
<li>More hardware = better performance</li>
<li>Pros: Cheaper; infinitely scalable</li>
<li>Cons: Harder to design, a bit harder to manage because you have more machines to watch</li>
</ul>
<h2>Database Replication</h2>
<p>Database replication is about automatically copying data from one database server to another database server. There is a <em>master</em> database that sends out data to it's <em>slaves</em> who copy it to their own database. The idea is that you can spread load to all of the slaves instead of relying on a single database server to handle the load of every user.</p>
<p>There are few types of database replication, but the most popular is called <em>master-slave</em> replication. This is where you have a single master database, and any number of slave databases. All <em>write</em> queries (queries that modify the database) go to the master. All <em>read</em> queries (SELECT type queries) are spread amongst the slaves. This works well in sites where the number of reads far outweigh the number of writes (which is just about every website online!).</p>
<h3>Designing for Database Replication</h3>
<p>For master-slave replication, your application must know which server to send queries to. This is fairly simple because it is easy to interpret plain-text SQL and know which kinds of queries are writes (UPDATE, INSERT, REPLACE) and which are reads (SELECT). For example, in your database abstraction layer you could modify your query method to read something like:</p>
<pre id="raw-php-51" style="display:none; width: 1px; height: 1px; overflow: hidden;">public function query($sql)
{
    if (preg_match('#^\s*SELECT#i', $sql)) {
        $conn = $this-&gt;slave_connection;
    } else {
        $conn = $this-&gt;master_connection;
    }

    // Send query to $conn database
    // ...
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-51" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-51'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-51">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public <span style="color:#000000; font-weight:bold;">function</span> query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'#^<span style="color:#000099; font-weight:bold;">\s</span>*SELECT#i'</span>, <span style="color:#0000FF;">$sql</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">slave_connection</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">master_connection</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Send query to $conn database</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The other thing you need to do is decide how you will load-balance the slave servers. There are hardware solutions or software solutions. The simplest method, and the one I personally like to use, is to simply choose one at random when your application starts up.</p>
<h3>Replication Lag</h3>
<p>Replication lag is the time it takes for all slaves to execute the write queries that happened on the master. This is usually very very fast (milliseconds) and thus not usually something to worry about. But if you are overloading your cluster, then the time it takes may increase and your users will start to see the affects. Things like saved changes not appearing when they reload the page.</p>
<p>One potential cause is that the slaves themselves are being overloaded -- they are serving too many read queries so they are falling behind. This can be fixed by adding new slaves to help spread the load some more.</p>
<h3>Database Engines</h3>
<p>One thing worth noting is that a slave can use a different database engine than its master. For example, your master can use MySQL InnoDB tables and your slaves can use MySQL MyISAM tables.</p>
<p>I mention this because InnoDB is required for transactions, but is much slower than MyISAM. So it seems natural to use InnoDB for the master (write) database and MyISAM for the slave (read) database. It means your slaves can handle more load.</p>
<h2>Database Partitioning</h2>
<p>Database partitioning is about splitting up your database so that all of the data is spread across multiple databases.</p>
<h3>Clustering</h3>
<p>Clustering is splitting your database up at the table level. So you have your users, usergroups and profiles on one database, and then you have your forums, topics and replies on another database. </p>
<p>This method is pretty straightforward but it is severely limited. It means you can't join on tables used in a different database, and it adds a certain overhead because each page now requires connections for each cluster. So if you have split your database into 4, one request now needs 4 connections.</p>
<p>Also, each cluster will likely have different requirements. For example, there will likely be many many more replies than there are user profiles. So this makes it more difficult to manage.</p>
<h3>Sharding</h3>
<p>Sharding is splitting data up at the row level. This means you have a bunch of databases with identical tables, but the data they contain is split up. For example, users 0-10000 are stored on shard 1, 10001-20000 are stored on shard 2 etc.</p>
<p>This method is infinitely scalable because when one database becomes overworked, you just add another shard.</p>
<p>The difficulty comes when you need to join with data that exists on different shards. The best way is to simply ensure that all related data exists together in a single shard -- but this can be pretty difficult. The other way is to denormalize your database so that you don't need to join with other tables.</p>
<h2>Database Denormalization</h2>
<p>Going against all best-practices with database design -- denormalization basically means copying data from one table into another to eliminate the need for table joins. Joins are fairly expensive, so it is sometimes worth the horrible icky feeling in your stomach when you denormalize.</p>
<p>So as an example, you might have a 'threads' table that has a field 'last_poster_id' that links to a 'users' record. If you wanted to get the username for the last poster, you'd need to join with the 'users' table. But if you denormalize your schema, the 'threads' table would have a new field called 'last_poster_username'.</p>
<p>The downside of denormalization is that you need to make sure all records stay consistent. With the example above, it means that if we change the username for the user we must also change every 'last_poster_username' in the 'threads' table. In situations where data is read many more times than it is written, this is usually okay.</p>
<h2>Caching</h2>
<p>The single most important aspect to scalable websites is caching. Every website, when you get right down to it, <em>reads</em> data more than it <em>writes</em> data. So, instead of reading data from a database -- which means reading from disk -- you should read data from a memory cache. With very popular websites, reading from disk is <em>complete and utter disaster</em>! You <em>must</em> cache to memory if you want to become the next YouTube.</p>
<h3>Cache what? How to cache?</h3>
<p>You should, in short, cache everything. The database is only there for persistent storage, and to refresh caches when the caches become stale. So that means your User::getUserinfo() method looks something like:</p>
<pre id="raw-php-52" style="display:none; width: 1px; height: 1px; overflow: hidden;">public function getUserinfo($user_id)
{
    if (cache_exists(&quot;user:$user_id&quot;)) {
        return cache_get(&quot;user:$user_id&quot;);
    }

    // Else, you need to fetch the data from the database
    // ...

    // And then cache the result for subsequent requests
    cache_save(&quot;user:$user_id&quot;, $userinfo);

    return $userinfo;
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-52" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-52'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-52">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public <span style="color:#000000; font-weight:bold;">function</span> getUserinfo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$user_id</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>cache_exists<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"user:$user_id"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> cache_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"user:$user_id"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Else, you need to fetch the data from the database</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// And then cache the result for subsequent requests</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; cache_save<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"user:$user_id"</span>, <span style="color:#0000FF;">$userinfo</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$userinfo</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>There are many different technologies that you can use to cache data. But the best of them (or at least most widely used) is called <a href="http://www.danga.com/memcached/">memcached</a>. PHP even has a <a href="http://php.net/manual/en/ref.memcache.php">memcached module</a>.</p>
<h3>Handling stale data</h3>
<p>When you are caching data, you need to be careful that the data in the cache is updated when the data in your database is updated. So this might mean a simple update to your User::save() method for example. Other times, you might add some logic so that cached data is automatically removed after a certain period (so that the next time its requested, it will be fetched and then recached once again).</p>
<h3>Where to cache</h3>
<p>Above, I mentioned that you should cache to memory. But I should clarify that memory is not the only way to cache things; and might be superfluous in many situations. Indeed, before you even need to think about memory caching you should identify a disk IO bottleneck so you know for sure that a memory cache will even improve the situation.</p>
<p>Before you move to a memory cache, the easiest way to implement a caching system is through files in the filesystem. You might even cache data in the database. The whole point in caching is to reduce load. So, for example, if you cached an entire page in the database all you need to do is fetch one row and you can present it to the user quickly (versus say, 10 queries and HTML-building routines you might normally need to do).</p>
<p>The point I'm trying to make is that caching isn't always about memory. It's about storing data (possibly pre-processed data) in a location that is inexpensive to access -- or at least inexpensive relative to the original source.</p>
<p>It just so happens that memory is the only source for a very popular site to read cache from, because all other disk-bound caches (including files and databases) become too slow.</p>
<h2>Separate Components</h2>
<p>You should separate components where you can so you can create specialized machines that are very good at certain tasks. For example, Apache is great for serving PHP pages but is really overkill if you need to serve up static CSS/Javascript files. Try adding a new server that runs Lighttpd to serve up those kinds of things. Additionally, don't put your MySQL server on the machine as your memcached server. You get the idea.</p>
<h2>In Closing...</h2>
<p>So there's a very brief overview of the topics you need to read about. I hope you now understand the sorts of things that go into a scalable website. My goal was to demystify the ideas of scalability so you can better research specifics; and of course in coming days I'll try and talk about specific techniques.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2008/10/28/overview-creating-scalable-websites/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Stream Wrappers</title>
		<link>http://devlog.info/2008/10/19/php-stream-wrappers/</link>
		<comments>http://devlog.info/2008/10/19/php-stream-wrappers/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 20:24:53 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Check It Out]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[stream-wrapper]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=57</guid>
		<description><![CDATA[If you talk to me regularly then you already know how much I love stream wrappers in PHP. The other day I was positively giddy with how easy it was to solve a particular problem by using stream wrappers. 
What are stream wrappers?
PHP 4.3.0 introduced the idea of streams. The PHP manual has a perfect [...]]]></description>
			<content:encoded><![CDATA[<p>If you talk to me regularly then you already know how much I love stream wrappers in PHP. The other day I was positively giddy with how easy it was to solve a particular problem by using stream wrappers. <span id="more-57"></span></p>
<h2>What are stream wrappers?</h2>
<p>PHP 4.3.0 introduced the idea of streams. The <a href="http://php.net/manual/en/intro.stream.php">PHP manual</a> has a perfect explanation:</p>
<blockquote><p>Streams were introduced with PHP 4.3.0 as a way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary locations within the stream. </p></blockquote>
<p>Streams are identified by a particular scheme in the URI. For example, with <code>http://someurl</code> the scheme is 'http' and when no scheme is provided like in <code>/file.php</code>, the scheme is defaulted to 'file' and is expected to be a file in the filesystem (thus <code>file:///file.php</code> would mean the same thing).</p>
<p><strong>Stream wrappers</strong> are bits of code that tell PHP how to handle certain schemes. The interesting thing is that you can create your own custom stream wrappers written in PHP with little effort.</p>
<p>For example, you may have done <code>file_get_contents('file.txt');</code> before. But say you wanted to have the same ease of use but with data from a different source. You could create a custom stream wrapper to get data from wherever you want and use all of the normal PHP functions as usual: <code>file_get_contents('mystream://whatever');</code>.</p>
<p>Almost <em>any</em> function that works on files will work with custom stream wrappers. That includes fopen, fwrite, fseek, is_readable, is_writable, stat, unlink and more. In essence you can create a virtual filesystem in your applications.</p>
<h2>Why create custom stream wrappers?</h2>
<p>So why in the world would you want to create custom stream wrappers? Basically it comes down to decoupling filesystem-coupled interfaces from the filesystem (or any <em>stream</em>, for that matter). That is, make software think it's talking to files when it's talking to your custom PHP class. It's all about abstraction!</p>
<p>You could also use stream wrappers to simplify an interface, though this is less common. If you are using stream wrappers to simplify an interface, you can just as easily create a normal <a href="http://en.wikipedia.org/wiki/Facade_pattern">facade</a> to simplify the interface and leave streams out of it.</p>
<h3>For example</h3>
<p>I wanted to use a newish PHP templating engine called <a href="http://dwoo.org/">Dwoo</a> for an upcoming project. The problem was it expects files everywhere. Template files were read from the filesystem and compiled to the filesystem, and caches were written to the filesystem. There was no easy way to change this.</p>
<p>For this particular project I need to read templates from a string (loaded from an arbitrary source decided at runtime), and I can't use the filesystem for caches, I need to use memcached. The reason being that the application will be run in a cluster. Using shared disks is not practical with this many machines, and disk IO would eventually become a bottleneck. True, we could use RAM disks, but it's a lot easier and portable to manage the data layer from our application instead of pushing it to the OS.</p>
<p>Using custom stream wrappers I created two new schemes for 'tpl://' (for reading) and 'tplc://' (for writing compiled templates). I was able to fill the requirements for the project while still using the awesome templating engine with little modification. The library still thinks it's reading and writing to files but behind the scenes it's reading from a cache of template sources and writing to memcached. Perfect!</p>
<h2>Creating custom stream wrappers</h2>
<p>To create your own custom stream wrappers you need to write a class that implements a set of methods. After you write the class, you can register it with <a href="http://php.net/manual/en/function.stream-wrapper-register.php">stream_wrapper_register</a>. Refer to that manual page for details on the methods you need.</p>
<p>The only problem I encountered was how to get my custom stream working with is_readable() and is_writable(). The trick is to implement the 'url_stat' method and set the 'mode' correctly. The mode is a number that represents what type of file the file is (file or directory etc) and the file permissions -- the usual chmod values. Here's a code snippet that accurately defines how the number should add up:</p>
<pre id="raw-php-54" style="display:none; width: 1px; height: 1px; overflow: hidden;">$dir = 040000;
$file = 0100000;

// Now you can add any permissions you want using the usual chmod octal notation:
$file += 0777; // A file that is readable
$dir += 0222; // A directory that is writable</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-54" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-54'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-54">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dir</span> = <span style="color:#CC66CC;color:#800000;">040000</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file</span> = <span style="color:#CC66CC;color:#800000;">0100000</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Now you can add any permissions you want using the usual chmod octal notation:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file</span> += <span style="color:#CC66CC;color:#800000;">0777</span>; <span style="color:#FF9933; font-style:italic;">// A file that is readable</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dir</span> += <span style="color:#CC66CC;color:#800000;">0222</span>; <span style="color:#FF9933; font-style:italic;">// A directory that is writable </span></div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2008/10/19/php-stream-wrappers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP and Unicode with UTF-8</title>
		<link>http://devlog.info/2008/08/24/php-and-unicode-utf-8/</link>
		<comments>http://devlog.info/2008/08/24/php-and-unicode-utf-8/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 03:35:02 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Application Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[character encoding]]></category>
		<category><![CDATA[character sets]]></category>
		<category><![CDATA[charsets]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=46</guid>
		<description><![CDATA[Now that I've already covered what Unicode is in another post, it's time to talk about actually using it. Today I'll talk about how to create PHP applications that correctly handle Unicode.
Why use Unicode
I covered this in my previous article on Unicode, but let's recap. The reason to use Unicode is so you can create [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I've already covered what Unicode is in <a href="http://devlog.info/2008/07/07/unicode-and-character-sets/">another post</a>, it's time to talk about actually using it. Today I'll talk about how to create PHP applications that correctly handle Unicode.<span id="more-46"></span></p>
<h2>Why use Unicode</h2>
<p>I covered this in my previous article on Unicode, but let's recap. The reason to use Unicode is so you can create <strong>multilingual applications</strong>.</p>
<p>Without Unicode you basically have two ways to serve a multilingual site. I'll briefly explain each here.</p>
<h3>Encoded HTML Entities</h3>
<p>The first way is to store all data in one character set (for example, iso-8895-1 for English) but encode all other foreign characters as HTML entities. Most web developers have probably done this at one time or another -- for example, using &amp;copy; for the copyright symbol. But you can also encode any character this way by specifying the Unicode code point like so: &amp;#0960; for &#0960;.</p>
<p><em>Common Uses</em>:</p>
<ul>
<li>When you have a page in one language, but sometimes need to use a character from another (perhaps you use the word über a lot ;-)).</li>
</ul>
<p><em>Advantages</em></p>
<ul>
<li>Easy to use.</li>
<li>Works with any character set you already have in place.</li>
</ul>
<p><em>Disadvantages</em></p>
<ul>
<li>It's not "correct". Instead of solving the problem, you're just patching it up.</li>
<li>Building systems that do not know about HTML or entities becomes troublesome. For example, sending emails or building a search engine.</li>
<li>As just normal text (not rendered HTML), the entity codes mean nothing. This becomes a problem if you have some way for users to consume your content other than through a web-browser (as already mentioned, email is a big one).</li>
</ul>
<h3>Multiple Encodings</h3>
<p>The second way is to store data in multiple encodings. Store English in ISO-8859-1, store Russian in ISO-8859-5 etc.</p>
<p><em>Common Use</em>:</p>
<ul>
<li>These days you see this kind of thing in places where systems are not ready to handle Unicode. For example, years ago some email providers or email clients did not handle Unicode at all. So a reason to mix encodings is for compatibility with legacy systems.</li>
<li>Another reason this is in use today is due to legacy code. It might be a big job to switch to using Unicode.</li>
</ul>
<p><em>Advantages</em>:</p>
<ul>
<li>Legacy systems usually have no problem working with any of the popular character sets.</li>
<li>Content is portable -- unlike using HTML entities which requires a parser to convert the entities.</li>
</ul>
<p><em>Disadvantages</em>:</p>
<ul>
<li>Impossible to mix characters from two different languages. For example, imagine you have a site that contains both Japanese and Russian articles. If you were to have some global site-map that lists all articles, the titles would be mangled because you can't represent both Cyrillic and Japanese characters with any single character set.</li>
<li>You introduce some complexity. Along with each piece of content, you need to store which character set was used. You need to worry about outputting the correct character set header when serving web pages. If you have some sort of service-based application, you need to worry about which character set requests are sent in and which one to send results back in.</li>
</ul>
<h2>Using Unicode</h2>
<p>Using Unicode suffers from none of the disadvantages describes above. Indeed, the very reason Unicode was created was to overcome those problems.</p>
<p>The most often used encoding for Unicode is <em>UTF-8</em>. This variable-length encoding stores Unicode characters in 1 to 4 bytes. There are other encodings, like UTF-16 or the depreciated UTF-7 -- but we'll only be talking about UTF-8 today. In fact, UTF-8 will probably be all you ever need.</p>
<p>Before we continue, I want to at least mention a few "problems" associated with using Unicode.</p>
<ul>
<li>Unicode will sometimes use more storage space. While normal ASCII characters will use the same amount of storage (1 byte), most other characters will use 2-4 bytes. Storage is cheap nowadays so this often isn't a problem.</li>
<li>Unicode is "new". The idea isn't new, but the widespread adoption is. (At least, relative to older character sets like ISO-8895-1 et all). This means that if your app interacts with very old systems a lot, then you might have some trouble with Unicode.
<p>You'll likely have no problems, but I mention this just to make sure you test any systems and devices your app needs to work with. For example, cell phones are notorious for their slow Unicode adoption rate.</li>
</ul>
<h2>PHP and UTF-8</h2>
<h3>Declaring UTF-8</h3>
<p>To start serving UTF-8 web pages, you need to send a content-type header. Web servers will automatically send a default header if you don't (which is why you may never have had to do this before). Most of the time the default header says the content is HTML, and is encoded in ISO-8895-1. Since you output HTML and your web pages are English -- this is almost always okay.</p>
<p>To start using Unicode, however, you need to either change the server config and change the default encoding or output it yourself from within PHP:</p>
<pre id="raw-php-66" style="display:none; width: 1px; height: 1px; overflow: hidden;">header('Content-Type: text/html; charset=utf-8');</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-66" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-66'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-66">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Content-Type: text/html; charset=utf-8'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Also note that your HTML content might also be spouting a 'http-equiv' meta tag that features the encoding. You should make sure it has UTF-8 there, too:</p>
<pre id="raw-html-67" style="display:none; width: 1px; height: 1px; overflow: hidden;">&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;</pre>
<div class="igBar">
<div class="wrap"><span id="lhtml-67" style="float:right"><a href="#" onclick="javascript:showCodeTxt('html-67'); return false;">Plain Text</a></span><span class="langName">HTML:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="html-67">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">"Content-Type"</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">"text/html; charset=utf-8"</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></a></span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>A browser will always take the HTTP header over the one in the meta tag, but it doesn't hurt to be consistent.</p>
<h3>Handling UTF-8</h3>
<p>It's all good to declare your pages as UTF-8, but that's useless if you're data isn't actually encoded in UTF-8. So let's talk about how to work with UTF-8 strings.</p>
<p>First of all, let me mention that browsers will submit forms in the same encoding as the page. So since you declared you pages as being encoded in UTF-8, all user data will be in UTF-8 too. The real work comes with using PHP to validate and manipulate user input.</p>
<h4>The Problem</h4>
<p>PHP is pretty much completely unaware of UTF-8 except in some specific circumstances. Most of the time, PHP treats all text as a string of 1 bytes -- that is, ASCII. This has a number of consequences due to the way UTF-8 uses a variable-length encoding scheme, and PHP's lack of functionality to properly handle it.</p>
<p>PHP's string functions are made to work on 1 byte characters. So counting the length of a string is as simple as checking how many bytes the string consumes, or getting a specific character by providing an offset is as simple as returning the byte at the offset. But stick in a UTF-8 encoded string, and now you have single characters that take up 2 (or 3 or 4) bytes. The result: PHP's string functions don't see a 2 byte character, they see 2 separate 1 byte characters (which is, of course, incorrect).</p>
<p>The solution is to write your own string functions that know all about UTF-8. You need to think about which functions will require characters to be 1 byte -- those are the dangerous ones. So things like strlen() or strpos(). Others, like explode(), only search for a series of bytes (which can be UTF-8) and use it -- so they are okay.</p>
<p>Here's a list of some unsafe UTF-8 functions:</p>
<ul>
<li>ord</li>
<li>str_ireplace</li>
<li>str_pad</li>
<li>str_split</li>
<li>strcasecmp</li>
<li>strcspn</li>
<li>stristr</li>
<li>strlen</li>
<li>strpos</li>
<li>strrpos</li>
<li>strrev</li>
<li>strspn</li>
<li>strtolower</li>
<li>strtoupper</li>
<li>substr_replace</li>
<li>substr_replace</li>
<li>[l|r]trim - Only unsafe when using the second $charlist argument, otherwise it's safe</li>
<li>ucfirst</li>
<li>ucwords</li>
</ul>
<h3>Libraries and Extensions</h3>
<p>You're pretty insane if you want to go ahead and re-write all of the string functions yourself! Instead, there are a few ways you can safely handle UTF-8 strings without all the headbanging.</p>
<ul>
<li><a href="http://www.php.net/iconv">iconv</a> which is default in PHP5, is mostly used when converting various character sets to UTF-8. However, there are a few string functions like iconv_strlen() that are UTF-8 aware. Not the holy grail..</li>
<li><a href="http://www.php.net/mbstring">mbstring</a> is another PHP extension that <em>is</em> intended to be the holy grail. But it's not in PHP by default, which can be a deal-breaker if you produce mass-market products that'll be used on shared servers. This extension has redefined all string functions so they are UTF-8 aware, and also offers some other features.</li>
<li><a href="http://phputf8.sourceforge.net/">phputf8</a> (<a href="http://sourceforge.net/projects/phputf8">download</a>) is a rewrite of the string functions written in PHP. It will auto-detect if iconv or mbstring are available and use those string functions where possible (since compiled string functions will be much faster than PHP string functions).</li>
</ul>
<p>I like to the phputf8 library in all my projects. It's simple and clean and it works!</p>
<h4>Well Formedness</h4>
<p>It is possible that UTF-8 input can be ill-formed. This is because of the variable-length encoding. To remove any ambiguity when parsing UTF-8 there are certain bytes that are invalid. These invalid ranges are a design feature, they make it impossible for the computer to "mix-up" a two-byte character for two separate one byte characters (or visa-versa).</p>
<p>You don't need to really understand what I just said since it has to do with how UTF-8 is represented on your computer (read the <a href="http://en.wikipedia.org/wiki/Utf-8">Wikipedia article</a> if you want to know more). All you need to understand is that UFT-8 text can be malformed, and thus, it's something you need to validate.</p>
<p>So when you are accepting form input, you should always make sure it's valid or else there's a possibility of various attacks. For example, imagine a UTF-F byte sequence is "AB", but an attacker modifies the request so only the first byte "A" is sent. Due to the way UTF-8 is encoded, your computer knows there should be a second byte after "A". So when you blindly output the single "A" into your HTML page, it's possible the next byte (whatever it may be) is "eaten up". Imagine if the next character is a double-quote intended to close off an HTML attribute or something like that. Possibility for XSS is introduced.</p>
<p>One very simple way to check for well formedness is to use preg_match() with the 'u' modifier:</p>
<pre id="raw-php-68" style="display:none; width: 1px; height: 1px; overflow: hidden;">if (strlen($str) AND !preg_match('/^.{1}/us', $str)) {
    die('Invalid UTF-8');
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-68" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-68'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-68">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$str</span><span style="color:#006600; font-weight:bold;">&#41;</span> AND !<a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'/^.{1}/us'</span>, <span style="color:#0000FF;">$str</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Invalid UTF-8'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
Since any invalid UTF-8 string will result in the match failing, all you need to do is match a single character.</p>
<p>If you're using phputf8, you can use the utf8_compliant() or utf8_is_valid() functions. utf8_compliant() uses the method described above, but will also pass 5 and 6 byte sequences which aren't technically valid UTF-8 (5 and 6 bytes are not unsafe, however). utf8_is_valid() will make sure the string is actually valid UTF-8 but will take longer.</p>
<h4>Regular Expressions</h4>
<p>When using regex with UTF-8, you need to use the 'u' modifier. That's all it takes!</p>
<pre id="raw-php-69" style="display:none; width: 1px; height: 1px; overflow: hidden;">if (preg_match('/myregex/u', $str)) {

}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-69" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-69'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-69">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'/myregex/u'</span>, <span style="color:#0000FF;">$str</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h4>Stick with ASCII when...</h4>
<p>When possible. There are still a lot of places where Latin letters and numbers are the only valid input. For example, URL's and email addresses.</p>
<p>Also, working with ASCII will often be better performing than using UTF-8. Especially if you are using a PHP-based library like phputf8 which will be slower still then compiled libraries like mbstring at certain operations.</p>
<h3>UTF-8 and MySQL</h3>
<p>If you use MySQL out of the box, and PHP's mysql_* functions, then you can INSERT and SELECT data from MySQL and you're safe almost all the time. That is, you can INSERT a string of UTF-8 characters into a table that is marked as ISO-8895-1. This is because MySQL simply sees a string of 1 byte characters -- it doesn't actually know that your text is UTF-8.</p>
<p>You'll immediately see a problem if you use a tool like phpMyAdmin, though. Because phpMyAdmin will output the results to you in the same charset as the database table. You'll end up seeing garbled text.</p>
<p>Two other problems will quickly become apparent:</p>
<ol>
<li>Collation. Collation is how a language is sorted. For example, in English we go from A-Z.</li>
<li>String functions. Using MySQL's string functions may garble your strings if the table charset and the data charset are different.</li>
</ol>
<p>What you should do is make sure your tables are encoded in UTF-8, and that the default collation is UTF-8, and that your MySQL client connection is being interpreted as UTF-8.</p>
<p>To set the global character set and collation you can edit the MySQL configuration file:</p>
<pre id="raw-code-70" style="display:none; width: 1px; height: 1px; overflow: hidden;">default-character-set=utf8
default-collation=utf8_general_ci</pre>
<div class="igBar">
<div class="wrap"><span id="lcode-70" style="float:right"><a href="#" onclick="javascript:showCodeTxt('code-70'); return false;">Plain Text</a></span><span class="langName">CODE:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="code-70">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">default-character-set=utf8 </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">default-collation=utf8_general_ci </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
This sets the charset to UTF-8 and the collation to case-insensitive UTF-8.</p>
<p>When creating a database or table, you can also supply these values:</p>
<pre id="raw-code-71" style="display:none; width: 1px; height: 1px; overflow: hidden;">CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test` (...) CHARACTER SET utf8 COLLATE utf8_general_ci</pre>
<div class="igBar">
<div class="wrap"><span id="lcode-71" style="float:right"><a href="#" onclick="javascript:showCodeTxt('code-71'); return false;">Plain Text</a></span><span class="langName">CODE:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="code-71">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CREATE TABLE `test` <span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span> CHARACTER SET utf8 COLLATE utf8_general_ci </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
Note that specific fields can also have their own charset and collation using the same syntax.</p>
<p>Once you connect to the MySQL server, you should issue a query to indicate that your queries will be in UTF-8:</p>
<pre id="raw-php-72" style="display:none; width: 1px; height: 1px; overflow: hidden;">mysql_query(&quot;SET NAMES 'utf8'&quot;);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-72" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-72'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-72">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"SET NAMES 'utf8'"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>After that -- you're good to go.</p>
<h3>Converting content to UTF-8</h3>
<p>First, a note: <em>Make sure you have enough storage for the new strings.</em> As explained earlier, UTF-8 strings can possibly use up to 4 times the amount of storage space. This usually isn't much of a problem with content saved to disk. However, if you are storing content in a database, your fields like VARCHAR(255) may be much too small.</p>
<p>If you need to convert simple strings on the go, you should use iconv. For example, perhaps you need to convert an email message from Windows-1251 (common with Russian) to UTF-8:</p>
<pre id="raw-php-73" style="display:none; width: 1px; height: 1px; overflow: hidden;">$content = iconv('Windows-1251', 'UTF-8', $old_content);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-73" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-73'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-73">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$content</span> = <a href="http://www.php.net/iconv"><span style="color:#000066;">iconv</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Windows-1251'</span>, <span style="color:#FF0000;">'UTF-8'</span>, <span style="color:#0000FF;">$old_content</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>If you are using MySQL, you can convert entire tables of content quite easily by issuing ALTER TABLE queries. Note that your existing charset and collation set on the tables need to be correct or else the translation will garble your text! (Going back to my note above about how MySQL won't know what charset you INSERT text in).</p>
<pre id="raw-code-74" style="display:none; width: 1px; height: 1px; overflow: hidden;">ALTER TABLE `table` CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE `table` CHANGE `table` `table` [TYPE] CHARACTER SET utf8 COLLATE utf8_general_ci</pre>
<div class="igBar">
<div class="wrap"><span id="lcode-74" style="float:right"><a href="#" onclick="javascript:showCodeTxt('code-74'); return false;">Plain Text</a></span><span class="langName">CODE:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="code-74">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ALTER TABLE `table` CHARACTER SET utf8 COLLATE utf8_general_ci</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ALTER TABLE `table` CHANGE `table` `table` <span style="color:#006600; font-weight:bold;">&#91;</span>TYPE<span style="color:#006600; font-weight:bold;">&#93;</span> CHARACTER SET utf8 COLLATE utf8_general_ci </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h3>Converting UTF-8 content into another character set</h3>
<p>Sometimes you might need to convert content back into another set. For example, if you've decided to switch to using UTF-8 but your app still communicates with a legacy system that only understands ISO-8895-1.</p>
<p>Again, you should use iconv which is perfectly suited for this kind of thing:</p>
<pre id="raw-php-75" style="display:none; width: 1px; height: 1px; overflow: hidden;">$content = iconv('UTF-8', 'ISO-8895-1', $old_content);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-75" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-75'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-75">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$content</span> = <a href="http://www.php.net/iconv"><span style="color:#000066;">iconv</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'UTF-8'</span>, <span style="color:#FF0000;">'ISO-8895-1'</span>, <span style="color:#0000FF;">$old_content</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Note that the characters represented in UTF-8 may not be able to be represented in the character set you want to convert to. For example, if you're content contained the copyright symbol, ISO-8895-1 won't have that symbol.</p>
<p>You have two options: You can either transliterate the characters that can't be converted or simply discard them.</p>
<p>Transliteration is the process of replacing the character with one that looks sort-of like it. For example, "é" might be replaced with "e". To achieve this, you append "//TRANSLIT" to the out-charset:</p>
<pre id="raw-php-76" style="display:none; width: 1px; height: 1px; overflow: hidden;">$content = iconv('UTF-8', 'ISO-8895-1//TRANSLIT', $old_content);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-76" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-76'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-76">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$content</span> = <a href="http://www.php.net/iconv"><span style="color:#000066;">iconv</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'UTF-8'</span>, <span style="color:#FF0000;">'ISO-8895-1//TRANSLIT'</span>, <span style="color:#0000FF;">$old_content</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>On the other hand if you just want to get rid of the offending characters, simply append "//IGNORE" to the out-charset.</p>
<h2>Conclusion</h2>
<p>I hope you've learned a bit about using UTF-8 with PHP. It's really not all that bad once you take in all of the information -- definitely not as scary as you thought it'd be, I bet!</p>
<p><strong>Additional reading:</strong></p>
<ul>
<li><a href="http://www.phpwact.org/php/i18n/charsets?s=utf8">WACT: Character Sets / Character Encoding Issues</a></li>
<li><a href="http://htmlpurifier.org/docs/enduser-utf8.html">UTF-8: The Secret of Character Encoding</a></li>
<li>Wikipedia on <a href="http://en.wikipedia.org/wiki/Utf-8">UTF-8</a> and <a href="http://en.wikipedia.org/wiki/Unicode">Unicode</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2008/08/24/php-and-unicode-utf-8/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

