<?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>ElbertF &#187; php</title>
	<atom:link href="http://elbertf.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Fri, 23 Jul 2010 09:12:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Escaping and sanitizing user input in PHP</title>
		<link>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/</link>
		<comments>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:12:09 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[escaping]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sanitizing]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=606</guid>
		<description><![CDATA[I recently answered a question on Quora, a questions and answers website that I frequent. The poster asked &#8220;what are best practices for escaping or sanitizing user input in PHP?&#8221; People seemed to appreciate the answer I wrote so I&#8217;ll post it here and elaborate on it a bit&#160;more. Why is it important to sanitize [...]]]></description>
			<content:encoded><![CDATA[<p>I recently answered a question on <a href="http://quora.com">Quora</a>, a questions and answers website that I frequent. The poster asked &#8220;<a href="http://www.quora.com/what-are-best-practices-for-escaping-or-sanitizing-user-input-in-PHP">what are best practices for escaping or sanitizing user input in PHP?</a>&#8221; People seemed to appreciate the answer I wrote so I&#8217;ll post it here and elaborate on it a bit&nbsp;more.</p>
<h4>Why is it important to sanitize user&nbsp;input?</h4>
<p>If you&#8217;re not careful with user input your website might be open to <a href="http://en.wikipedia.org/wiki/Code_injection">code injection</a>, <a href="http://en.wikipedia.org/wiki/Directory_traversal">directory traversal</a> or similar attacks. Information supplied by users can never be assumed&nbsp;safe.</p>
<p>Examples of user input are submitted forms (e.g. comments), URL parameters (?q=example) and server-side scripts pulling in third-party data, such as an RSS feed&nbsp;importer.</p>
<p><span id="more-606"></span></p>
<h4>HTML and&nbsp;JavaScript</h4>
<p>To make strings safe for HTML (without breaking <a href="http://www.unicode.org/standard/WhatIsUnicode.html">Unicode text</a>) use&nbsp;<a href="http://php.net/manual/en/function.htmlentities.php"><code>htmlentities()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsafe</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will encode all special HTML characters. This method is better than black-listing specific elements such as &lt;script&gt; or just opening tags (&lt;). Do not use <a href="http://php.net/manual/en/function.strip-tags.php"><code>strip_tags()</code></a>, <a href="http://php.net/manual/en/function.str-replace.php"><code>str_replace()</code></a> or <a href="http://www.php.net/manual/en/book.pcre.php">regular expressions</a> to filter HTML and JavaScript, it is easy to miss obscure vulnerabilities and leave them&nbsp;exploitable.</p>
<p>When automatically parsing a URL to display a clickable link, check if URL starts with a protocol like &#8220;http://&#8221; (regex: <code>/[a-z]:\/\//i</code>) and make sure <code>javascript:</code> links never work. Also be careful with quotes and closing angle brackets as they can break&nbsp;HTML.</p>
<p>These examples demonstrate how unfiltered links can be&nbsp;dangerous:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'javascript:alert(\'XSS\');'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://example.com&quot; onclick=&quot;alert(\'XSS\');'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'example.com&quot;&gt;&lt;script&gt;alert(\'XSS\');&lt;/script&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;Click here&lt;/a&gt;</pre></td></tr></table></div>

<h4>URLs</h4>
<p>To pass values as a parameter to a URL, use&nbsp;<a href="http://php.net/manual/en/function.rawurlencode.php"><code>rawurlencode()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://example.com/?k='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This function is nearly identical to <a href="http://php.net/manual/en/function.urlencode.php"><code>urlencode()</code></a> but follows the <a href="http://www.faqs.org/rfcs/rfc1738.html">RFC 1738</a>&nbsp;specification.</p>
<h4>MySQL database&nbsp;queries</h4>
<p>When storing strings in a MySQL database, use&nbsp;<a href="http://php.net/manual/en/function.mysql-real-escape-string.php"><code>mysql_real_escape_string()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$safe</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsafe</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Often <a href="http://php.net/manual/en/function.addslashes.php"><code>addslashes()</code></a> is used instead but this is not enough to prevent <a href="http://www.owasp.org/index.php/SQL_Injection">SQL injection attacks</a>. If you want to learn why I recommend reading <a href="http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string">this blog post</a> by Chris Shiflett and the third chapter of the <a href="http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf">guide to PHP security</a> by Ilia Alshanetsky (PDF,&nbsp;130KB).</p>
<p>This function requires a MySQL&nbsp;connection.</p>
<h4>Magic&nbsp;Quotes</h4>
<p>If your web app suffers from unwanted backslashes appearing in content this is probably due to double escaping (e.g. &#8220;today&#92;&#8217;s weather&#8221;). This is likely caused by PHP&#8217;s now deprecated <a href="http://php.net/manual/en/security.magicquotes.php">Magic&nbsp;Quotes</a>.</p>
<p>Magic Quotes is a feature automatically escapes user input, intended to help beginners write more secure code. Because it&#8217;s not always on or needed this affects portability and requires excessive use of <a href="http://www.php.net/manual/en/function.stripslashes.php">stripslashes()</a> to&nbsp;undo.</p>
<p>To recursively undo the affects of Magic Quotes use this function at the beginning of your&nbsp;scripts:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> undo_magic_quotes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_magic_quotes_gpc'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$_GET</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_POST</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_COOKIE</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'undo_magic_quotes'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Directories</h4>
<p>And finally, be careful with including files using user&nbsp;input.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Don't do this</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// And especially not this</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is dangerous because a user can request any file from the server and execute or view the code (e.g. &#8220;?file=../../config.php&#8221;, this is called a <a href="http://www.owasp.org/index.php/Category:Path_Traversal_Attack">path traversal attack</a>). One solution is to use <a href="http://php.net/manual/en/function.basename.php"><code>basename()</code></a> which strips off the path of a file&nbsp;name.</p>
<p>These were just a few basics, there is a lot more to web application security. <a href="http://www.owasp.org">OWASP</a> is a great resource if you want to learn&nbsp;more.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2010/07/escaping-and-sanitizing-user-input-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to store passwords safely with PHP and MySQL</title>
		<link>http://elbertf.com/2010/01/store-passwords-safely-with-php-and-mysql/</link>
		<comments>http://elbertf.com/2010/01/store-passwords-safely-with-php-and-mysql/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 08:43:27 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=447</guid>
		<description><![CDATA[First, let me tell you how not to store passwords and&#160;why. Do not store password as plain&#160;text This should be obvious. If someone gains access to your database then all user accounts are compromised. And not only that, people tend to use the same password on different sites so those accounts will be compromised as [...]]]></description>
			<content:encoded><![CDATA[<p>First, let me tell you how <strong>not</strong> to store passwords and&nbsp;why.</p>
<h4>Do not store password as plain&nbsp;text</h4>
<p>This should be obvious. If someone gains access to your database then all user accounts are compromised. And not only that, people tend to use the same password on different sites so those accounts will be compromised as well. Your site doesn&#8217;t even need to be hacked; a system administrator could easily browse your&nbsp;database.</p>
<h4>Do not try to invent your own password&nbsp;security</h4>
<p>Chances are that you&#8217;re no security expert. You&#8217;re better off using a solution that has been proven to work instead of coming up with something&nbsp;yourself.</p>
<h4>Do not encrypt&nbsp;passwords</h4>
<p><a href="http://en.wikipedia.org/wiki/Encryption">Encryption</a> may seem like a good idea but the process is reversible. Anyone with access to your code would have no trouble transforming the passwords back to their originals. <a href="http://en.wikipedia.org/wiki/Security_through_obscurity">Security through obscurity</a> is not&nbsp;sufficient!</p>
<p><span id="more-447"></span></p>
<h4>Do not use&nbsp;MD5</h4>
<p>Storing password <a href="http://en.wikipedia.org/wiki/Hash_function">hashes</a> is a step in the right direction. Cryptographic hashing functions like <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> are irreversible which makes it difficult to figure out the original password. To validate a hashed password, simply hash the password again when a user logs in and compare the&nbsp;hashes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'swordfish'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Value: 15b29ffdce66e10527a65bc6d71ad94d</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note that this makes it impossible to retrieve a password from the database. If a user forgets his password, simply generate a new&nbsp;one.</p>
<p>So why not MD5? It is quite easy to make a list of millions of hashed passwords (a <a href="http://en.wikipedia.org/wiki/Rainbow_table">rainbow table</a>) and compare the hashes to find the original passwords (the same goes for other hashing functions like&nbsp;<a href="http://en.wikipedia.org/wiki/SHA_hash_functions">SHA-1</a>).</p>
<p>MD5 is also prone to <a href="http://en.wikipedia.org/wiki/Brute_force_attack">brute forcing</a> (trying out all combinations with an automated script) because of <a href="http://en.wikipedia.org/wiki/Collision_resistance">collisions</a>. This means that different passwords can have the same hash, making it even easier to find one that&nbsp;works.</p>
<p>MD5 collision demo:&nbsp;<a href="http://www.mscs.dal.ca/~selinger/md5collision/">mscs.dal.ca/~selinger/md5collision</a></p>
<h4>Do not use a single site-wide&nbsp;salt</h4>
<p>A <a href="http://en.wikipedia.org/wiki/Salt_%28cryptography%29">salt</a> is a string that is hashed together with a password so that most rainbow tables (or <a href="http://en.wikipedia.org/wiki/Dictionary_attack">dictionary attacks</a>) won&#8217;t&nbsp;work.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'swordfish'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'something random'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Value: db4968a3db5f6ed2f60073c747bb4fb5</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is better then using just MD5 but someone with access to your code can find the salt a generate a new rainbow&nbsp;table.</p>
<h4>What you should&nbsp;do</h4>
<ul>
<li>Use a cryptographically strong hashing function like SHA-1 or even SHA-256 (see PHP&#8217;s <a href="http://www.php.net/manual/en/function.hash.php">hash()</a>&nbsp;function).</li>
<li>Use a long and random salt for each&nbsp;password.</li>
<li>Use a slow hashing algorithm to make brute force attacks near&nbsp;impossible.</li>
<li>Regenerate the hash every time a users logs&nbsp;in.</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Admin'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'gf45_gdf#4hg'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a 256 bit (64 characters) long random salt</span>
<span style="color: #666666; font-style: italic;">// Let's add 'something random' and the username</span>
<span style="color: #666666; font-style: italic;">// to the salt as well for added security</span>
<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha256'</span><span style="color: #339933;">,</span> <span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'something random'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Prefix the password with the salt</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Hash the salted password a bunch of times</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha256'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Prefix the hash with the salt so we can find it back later</span>
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Value:
 * e31f453ab964ec17e1e68faacbb64f05bccceb179858b4c482c1b182ff1e440e
 * f1e10feb5b86c6d367e4eb8f90f2cde5648a7db3df8526878f20a77eed00c703
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In the above example we turned a reasonably strong password into a 128 characters long hash that we can store in a database. The next time the user logs in we can validate the password as&nbsp;follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Admin'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'gf45_gdf#4hg'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
    SELECT
        `hash`
    FROM `users`
        WHERE `username` = &quot;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;
    LIMIT 1
    ;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The first 64 characters of the hash is the salt</span>
<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hash'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Hash the password as we did before</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">hash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha256'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$salt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$hash</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hash'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Ok!</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>A few additional tips to prevent user accounts from being&nbsp;hacked:</p>
<ul>
<li>Limit the number of failed login&nbsp;attempts.</li>
<li>Require strong&nbsp;passwords.</li>
<li>Do not limit passwords to a certain length (remember, you&#8217;re only storing a hash so length doesn&#8217;t&nbsp;matter).</li>
<li>Allow special characters in passwords, there is no reason not&nbsp;to.</li>
</ul>
<p>That&#8217;s it, happy&nbsp;coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2010/01/store-passwords-safely-with-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Swiftlet 1.0.0 Stable</title>
		<link>http://elbertf.com/2009/05/swiftlet-100-stable/</link>
		<comments>http://elbertf.com/2009/05/swiftlet-100-stable/#comments</comments>
		<pubDate>Fri, 08 May 2009 02:51:32 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[pintail]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=164</guid>
		<description><![CDATA[Swiftlet, the light-weight PHP framework that I&#8217;ve been working on for a while, is now&#160;stable. Feature-wise not much has changed since the Beta and Release Candidate cycles but the code has been thoroughly tested and improved where possible. If you&#8217;re planning on building a PHP website, give Swiftlet a&#160;try. I moved the project page and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/><a href="http://swiftlet.org/">Swiftlet</a>, the light-weight PHP framework that I&#8217;ve been working on for a while, is now&nbsp;stable.</p>
<p>Feature-wise not much has changed since the Beta and Release Candidate cycles but the code has been thoroughly tested and improved where possible. If you&#8217;re planning on building a PHP website, <a href="http://code.google.com/p/swiftlet/downloads/list">give Swiftlet a&nbsp;try</a>.</p>
<p>I moved the project page and documentation away from Google Code, if you go to <a href="http://swiftlet.org/">swiftlet.org</a> you&#8217;ll find the new page. It&#8217;s powered by a documentation system that I custom coded (dubbed Pintail). If there is any interest I will release the code behind it as Open-Source as&nbsp;well.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/swiftlet-100-stable/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Swiftlet Beta</title>
		<link>http://elbertf.com/2009/05/swiftlet-beta/</link>
		<comments>http://elbertf.com/2009/05/swiftlet-beta/#comments</comments>
		<pubDate>Sat, 02 May 2009 08:17:52 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=156</guid>
		<description><![CDATA[Swiftlet is now in beta, after 15 alpha cycles I&#8217;m confident that it&#8217;s now pretty much feature complete (there core that is, there will be plenty more plug-ins) and relatively&#160;stable. One of the most important recently added features is the plug-in installer. It checks for compatibility with the core code and creates and populates database [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/><a href="http://swiftlet.org">Swiftlet</a> is now in beta, after 15 alpha cycles I&#8217;m confident that it&#8217;s now pretty much feature complete (there core that is, there will be plenty more plug-ins) and relatively&nbsp;stable.</p>
<p>One of the most important recently added features is the plug-in installer. It checks for compatibility with the core code and creates and populates database tables with a click of the mouse. Plug-ins that don&#8217;t require a database connection don&#8217;t need to be installed; they&#8217;re <em>plug-and-play</em> (and Swiftlet runs fine without a&nbsp;database).</p>
<p>I also added plug-ins to handle user sessions and authorization. This should make it easy to create a website that requires a login&nbsp;system.</p>
<p>Download:&nbsp;<a href="http://code.google.com/p/swiftlet/downloads/list">code.google.com/p/swiftlet/downloads/list</a></p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/swiftlet-beta/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Swiftlet — light-weight PHP framework</title>
		<link>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/</link>
		<comments>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 03:48:55 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=132</guid>
		<description><![CDATA[I just released an early alpha version of Swiftlet, an Open Source, light-weight PHP framework released under the GPL&#160;license. It&#8217;s targeted at developers who want to built simple websites that don&#8217;t require large and complex frameworks, but do want a solid base to work from. Swiftlet provides basic security features such as user input sanitizing, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://code.google.com/p/swiftlet/logo?logo_id=1240554537" alt="Swiftlet logo" class="left"/>I just released an early alpha version of <a href="http://swiftlet.org">Swiftlet</a>, an Open Source, light-weight PHP framework released under the <a href="http://www.gnu.org/licenses/gpl.html">GPL&nbsp;license</a>.</p>
<p>It&#8217;s targeted at developers who want to built simple websites that don&#8217;t require large and complex frameworks, but do want a solid base to work from. Swiftlet provides basic security features such as user input sanitizing,  is highly extensible thanks to the deeply integrated hook system, completely Object Oriented and separates logic from design&nbsp;(MVC).</p>
<p>Even the most basic features such as connecting to a database and output buffering are implemented as plug-ins. This means they can be modified, extended and removed without hacking into the core&nbsp;code.</p>
<p>Website:&nbsp;<a href="http://swiftlet.org/">http://swiftlet.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/04/swiftlet-%e2%80%94-light-weight-php-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: How not to pollute the global scope</title>
		<link>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/</link>
		<comments>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 04:04:27 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=62</guid>
		<description><![CDATA[An emerging trend in JavaScript is to wrap entire programs in a single object to prevent conflicts with other scripts. The same can be done with PHP, something I haven&#8217;t seen done very&#160;often. If you ever want to combine frameworks you may discover that they use similar names for some variables and functions. This can [...]]]></description>
			<content:encoded><![CDATA[<p>An emerging trend in JavaScript is to wrap entire programs in a single object to prevent conflicts with other scripts. The same can be done with PHP, something I haven&#8217;t seen done very&nbsp;often.</p>
<p><span id="more-62"></span></p>
<p>If you ever want to combine frameworks you may discover that they use similar names for some variables and functions. This can lead to unexpected output and security holes. If you&#8217;re planning on writing a framework of your own, consider wrapping it in an&nbsp;object.</p>
<p>In PHP5 it&#8217;s possible to pass objects by reference which means you can send an instance of a class to another class. This way you can nest classes and access the&nbsp;parent.</p>
<p><strong>core.php:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> core
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a global variable.'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">const</span>
        CONST_VAR <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a constant.'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// This function is executed when core is initialized.</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'classes/foo.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// All the main functions go here.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>foo.php:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$core</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$core</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Reference to the core object.</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">core</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$core</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> hello_world<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example &#8220;core&#8221; is the site&#8217;s main object containing global variables and functions. When we create an instance of class &#8220;foo&#8221;, we send a reference to the core object (&#8220;$this&#8221;) as a parameter. On a regular page the classes, variables and function can be accessed like&nbsp;this:</p>
<p><strong>page.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'classes/core.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$core</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> core<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">var</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output: This is a global variable.</span>
<span style="color: #b1b100;">echo</span> core<span style="color: #339933;">::</span><span style="color: #004000;">CONST_VAR</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output: This is a constant.</span>
&nbsp;
<span style="color: #000088;">$core</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello_world</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output: This is a global variable.</span></pre></div></div>

<p>This way there is only one variable in the global scope: $core. Another advantage is that all variables declared in core are global and accessible throughout the whole&nbsp;program.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/03/php-how-not-to-pollute-the-global-scope/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Tips for writing compact PHP code</title>
		<link>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/</link>
		<comments>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 08:34:16 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=30</guid>
		<description><![CDATA[Writing compact code can save you time. It&#8217;s not always recommended and often even strongly discouraged as it makes your code less readable, but for simple operations it can be more efficient. In this post I will give a few&#160;examples. 1. Drop&#160;braces Braces aren&#8217;t required in control structures with only one expression. Sometimes it makes [...]]]></description>
			<content:encoded><![CDATA[<p>Writing compact code can save you  time. It&#8217;s not always recommended and often even strongly discouraged as it makes your code less readable, but for simple operations it can be more efficient. In this post I will give a few&nbsp;examples.</p>
<p><span id="more-30"></span></p>
<h4>1. Drop&nbsp;braces</h4>
<p>Braces aren&#8217;t required in control structures with only one expression. Sometimes it makes sense to drop them. It&#8217;s <em>very easy to make mistakes</em> if you ever add code to the structure, I recommend only to do this when the expression is short and fits on a single&nbsp;line.</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$module</span> <span style="color: #009900;">&#41;</span> load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>           error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>2. Use ternary&nbsp;operators</h4>
<p>The above example can be made even more compact using <a href="http://en.wikipedia.org/wiki/%3F:">ternary&nbsp;operators</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$module</span> ? load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No module'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>3. Use &#8220;OR&#8221; instead of&nbsp;&#8220;IF&#8221;</h4>
<p>&#8220;OR&#8221; is the same as &#8220;or&#8221; and&nbsp;&#8221;||&#8221;.</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>4. Don&#8217;t compare variables to&nbsp;booleans</h4>
<p>&#8220;if ( $foo == TRUE )&#8221; is the same as &#8220;if ( $foo )&#8221;. This shortcut can make your life as a programmer much&nbsp;easier:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> has_value<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$var</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> has_value<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> bool <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>5. Use default values for&nbsp;variables</h4>
<p>It&#8217;s usually a good idea to define important variables at the beginning of your script, instead of inside control structures (this could result in undefined variables later on). Another advantage is that you can often save an entire else-block as demonstrated in this&nbsp;example:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is greater then 3.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is lower than or equal to 3.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is lower than or equal to 3.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$foo is greater then 3.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>6. Assign variables inside&nbsp;conditions</h4>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$contents</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>7. Group variable&nbsp;declarations</h4>
<p>Instead of prefixing every single variable declaration in a class with &#8220;public&#8221;, &#8220;protected&#8221; or &#8220;private&#8221; keywords, group&nbsp;them:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> db
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$tables</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> db
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span>
        <span style="color: #000088;">$query</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$result</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$tables</span>
        <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Something similar can be done with regular variable declarations if they need to assign them the same&nbsp;value:</p>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span>    <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span>    <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span>    <span style="color: #339933;">=</span>
<span style="color: #000088;">$bar</span>    <span style="color: #339933;">=</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>8. Merge arrays instead of assigning individual&nbsp;keys</h4>
<p><strong>Long:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'item 5'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Short:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 5'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Even&nbsp;shorter:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item 2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$items</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'item 3'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 4'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'item 5'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it! Please share this post if you found it&nbsp;useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/03/tips-for-writing-compact-php-code/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
