<?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; programming</title>
	<atom:link href="http://elbertf.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Sun, 15 Apr 2012 21:46:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Swiftlet 3.0</title>
		<link>http://elbertf.com/2012/01/swiftlet-3-0-easy-mvc-framework/</link>
		<comments>http://elbertf.com/2012/01/swiftlet-3-0-easy-mvc-framework/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 05:38:00 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[app updates]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[micro framework]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[swiftlet]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=1027</guid>
		<description><![CDATA[Three years ago I released an early version of Swiftlet, a light-weight PHP framework built for speed and security. Over time however, a lot of functionality was added and started to suffer from feature creep. My original intention of creating a maintainable framework that was easy to pick up was a bit&#160;lost. Last Sunday I [...]]]></description>
			<content:encoded><![CDATA[<p>Three years ago I released an early version of <a href="http://swiftlet.org/">Swiftlet</a>, a light-weight PHP framework built for speed and security. Over time however, a lot of functionality was added and started to suffer from <a href="http://en.wikipedia.org/wiki/Feature_creep">feature creep</a>. My original intention of creating a maintainable framework that was easy to pick up was a bit&nbsp;lost.</p>
<p>Last Sunday I had a spur of inspiration and decided to start over. With a slightly different vision, a better understanding of <a href="http://en.wikipedia.org/wiki/Model_view_controller">MVC</a> and experience gained with the project I managed to finish it the same day. The result is about 150kB of code including&nbsp;documentation.</p>
<p>If you&#8217;re unfamiliar with the <a href="http://en.wikipedia.org/wiki/Model_view_controller">model-view-controller</a> approach to programming Swiftlet might be a good starting point. The code is quite simple and hopefully the documentation makes the concept&nbsp;understandable.</p>
<h4>Source&nbsp;code</h4>
<p><a href="http://swiftlet.org/">Swiftlet on&nbsp;GitHub</a></p>
<p>Swiftlet is released under the <a href="http://www.gnu.org/licenses/gpl-3.0.txt">GNU Public&nbsp;License</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2012/01/swiftlet-3-0-easy-mvc-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Free transform plugin for Raphaël JS</title>
		<link>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/</link>
		<comments>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 04:35:32 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mit]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[raphaël]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[vector graphics]]></category>
		<category><![CDATA[vml]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=992</guid>
		<description><![CDATA[After doing some research for work involving client-side image manipulation I decided to dive into Raphaël, a JavaScript library that simplifies working with vector graphics on the&#160;web. One thing I struggled with was applying multiple transformations to an element. When rotating an object the coordinate plane rotates with it which means dragging no longer works [...]]]></description>
			<content:encoded><![CDATA[<p>After doing some research for work involving client-side image manipulation I decided to dive into <a href="http://raphaeljs.com/">Raphaël</a>, a JavaScript library that simplifies working with vector graphics on the&nbsp;web.</p>
<p>One thing I struggled with was applying multiple transformations to an element. When rotating an object the coordinate plane rotates with it which means dragging no longer works as one might expect (e.g. right becomes up). Raphaël&#8217;s creator <a href="http://dmitry.baranovskiy.com/">Dmitri Baranovskiy</a> indicated this is the <a href="https://github.com/DmitryBaranovskiy/raphael/issues/102#issuecomment-2725489">correct&nbsp;behavior</a>.</p>
<p>I worked out a solution and released it as a plugin. It supports snap-to-grid dragging, scaling and rotating and handles sets. Like Raphaël itself, the plugin is licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT&nbsp;license</a>.</p>
<h4>Source&nbsp;code</h4>
<p><a href="https://github.com/ElbertF/Raphael.FreeTransform">Raphaël.FreeTransform on&nbsp;GitHub</a></p>
<h4>Demo</h4>
<p><a href="http://elbertf.com/raphael/free_transform/">elbertf.com/raphael/free_transform/</a></p>
<h3>See&nbsp;also&hellip;</h3>
<p>These are a couple of other plugins I created&nbsp;recently.</p>
<h4><a href="https://github.com/ElbertF/Raphael.JSON">Raphaël.JSON</a></h4>
<p>Serialize Raphaël elements and&nbsp;back.</p>
<h4><a href="https://github.com/ElbertF/Raphael.Export">Raphaël.Export</a></h4>
<p>Cross-browser solution to export Raphaël elements to <a href="http://www.w3.org/Graphics/SVG/">SVG</a>, works in Internet Explorer 6, 7 and 8. Can be used to convert Raphaël drawings to PNG or PDF server-side using <a href="http://www.imagemagick.org/">ImageMagick</a> or&nbsp;<a href="http://xmlgraphics.apache.org/batik/" title="Batik SVG Toolkit">Batik</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Resolve scoping issues in jQuery with anonymous functions</title>
		<link>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/</link>
		<comments>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/#comments</comments>
		<pubDate>Sat, 21 May 2011 05:56:37 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=943</guid>
		<description><![CDATA[I had a bit of a head-scratching moment recently when I was using jQuery to bind event handlers to elements in a loop. Usually I would just use jQuery&#8217;s .each() method but in this specific case that wasn&#8217;t an option. Consider the following (simplified)&#160;code: 1 2 3 4 5 6 7 8 9 10 11 [...]]]></description>
			<content:encoded><![CDATA[<p>I had a bit of a head-scratching moment recently when I was using <a href="http://jquery.com/">jQuery</a> to bind event handlers to elements in a loop. Usually I would just use jQuery&#8217;s <a href="http://api.jquery.com/each/"><code>.each()</code></a> method but in this specific case that wasn&#8217;t an option. Consider the following (simplified)&nbsp;code:</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: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-1&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-2&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-3&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#item-'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You clicked item '</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</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;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Against my intuition, clicking any of the three <code>li</code> elements would return return &#8220;You clicked item 4&#8221;. I then realized that <code>i</code> was being read after the for-loop had finished. To solve this, wrap the function in another <a href="http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work">anonymous function</a> passing <code>i</code> as an&nbsp;argument:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-1&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-2&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-3&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#item-'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You clicked item '</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</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;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>This way the second <code>i</code> lives in the same scope as the <code>alert()</code> function which will now return the correct&nbsp;value.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>4</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>30</slash:comments>
		</item>
		<item>
		<title>Safer web forms with security tokens</title>
		<link>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/</link>
		<comments>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 08:24:31 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=367</guid>
		<description><![CDATA[A common issue with many web applications is their vulnerability for Cross-Site Request Forgery, or XSRF. It allows a hacker to send a malicious request to a website with an other user&#8217;s privileges. Here&#8217;s how it&#160;works: A hacker creates a page with a form that submits data to&#160;example.com. An administrator from example.com is tricked into [...]]]></description>
			<content:encoded><![CDATA[<p>
A common issue with many web applications is their vulnerability for <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-Site Request Forgery</a>, or XSRF. It allows a hacker to send a malicious request to a website with an other user&#8217;s privileges. Here&#8217;s how it&nbsp;works:
</p>
<ul>
<li>
A hacker creates a page with a form that submits data to&nbsp;<em>example.com</em>.
</li>
<li>
An administrator from <em>example.com</em> is tricked into visiting the page, the form is submitted using&nbsp;JavaScript.
</li>
<li>
The data is handled by <em>example.com</em> as if it came from the administrator (because it&nbsp;did).
</li>
</ul>
<p>
This allows a hacker to perform administrative tasks on <em>example.com</em> like editing pages or deleting&nbsp;users.
</p>
<p><span id="more-367"></span></p>
<h4>Solution</h4>
<p>
Probably the best solution to this problem is using a <a href="http://en.wikipedia.org/wiki/Security_token">security token</a>. This is a code (usually a <a href="http://en.wikipedia.org/wiki/Hash_function">hash</a>) that is send with the form in a hidden field and is only valid for a specific user and a certain period of&nbsp;time.
</p>
<p>
I recommend using a <a href="http://en.wikipedia.org/wiki/SHA_hash_functions">SHA1</a> hash created from these&nbsp;components:
</p>
<ul>
<li>
Information about the user (IP address, user agent,&nbsp;username).
</li>
<li>
Information about the server (hostname, software&nbsp;version).
</li>
<li>
Information about the website (database name, table&nbsp;prefix).
</li>
<li>
Information that expires (user&#8217;s session&nbsp;id).
</li>
</ul>
<p>
This will result in an unpredictable and seemingly random hash that is still verifiable by the server and difficult to&nbsp;fake.
</p>
<p>
After the form is submitted the hash is re-created and compared to the token that was send with the form. Only if the hashes match the form is processed, otherwise an error message is displayed. This way even if a hacker manages to find the user&#8217;s token the request will&nbsp;fail.
</p>
<p><strong>Example&nbsp;(PHP/HTML):</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</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;">$authToken</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>
  <span style="color: #990000;">phpversion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$dbName</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span>
  <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'auth_token'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'auth_token'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$authToken</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Process form</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Display error message</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;form id=&quot;form&quot; method=&quot;post&quot; action=&quot;./&quot;&gt;
  &lt;fieldset&gt;
    &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; value=&quot;&quot;/&gt;
  &lt;/fieldset&gt;
  &lt;fieldset&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;auth_token&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$authToken</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;/&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Note that this only really works for POST requests. For this reason GET (regular links) should never be used to pass information that is used for administrative tasks. AJAX requests should also <em>always</em> use&nbsp;POST.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/11/safer-web-forms-with-security-tokens/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using Internet Explorer 8 for IE6 and IE7 testing</title>
		<link>http://elbertf.com/2009/10/using-internet-explorer-8-for-ie6-and-ie7-testing/</link>
		<comments>http://elbertf.com/2009/10/using-internet-explorer-8-for-ie6-and-ie7-testing/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 23:06:54 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[microsoft]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=342</guid>
		<description><![CDATA[I personally don&#8217;t care what my websites look like in IE6 or 7 (or 8) but for those who do there is neat little trick to test your site for these&#160;browsers. The hard&#160;way If you&#8217;re on Windows XP or older you can use a standalone version of IE6. If you&#8217;re on Windows 7 (not the [...]]]></description>
			<content:encoded><![CDATA[<p>I personally don&#8217;t care what my websites <a href="http://elbertf.com/ie6ify">look like in IE6</a> or 7 (or 8<!---->) but for those who do there is neat little trick to test your site for these&nbsp;browsers.</p>
<h4>The hard&nbsp;way</h4>
<p>If you&#8217;re on Windows XP or older you can use a <a href="http://tredosoft.com/Multiple_IE">standalone version of IE6</a>. If you&#8217;re on Windows 7 (not the beta though), you can use Microsoft&#8217;s free &#8220;<a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=21EABB90-958F-4B64-B5F1-73D0A413C8EF&#038;displaylang=en">IE Application Compatibility VPC Image</a>&#8221; to run XP with IE6 or 7 in a virtual machine. If you&#8217;re on any other operating system (Windows Vista, Mac OS, Linux) and have a Windows XP license you can run it using a VM like&nbsp;<a href="http://www.virtualbox.org/">VirtualBox</a>.</p>
<h4>The easy&nbsp;way</h4>
<p>If you&#8217;re already running IE8 there is an easier way. <a href="http://www.microsoft.com/windows/internet-explorer/features/easier.aspx">Compatibility View</a> will render pages as IE7, and a missing <a href="http://www.w3.org/QA/Tips/Doctype">doctype</a> will cause pages to be rendered as IE6 in <a href="http://en.wikipedia.org/wiki/Quirks_mode">quirks&nbsp;mode</a>.</p>
<p>You can simply remove the doctype from your pages when you&#8217;re testing for IE6 or ― if you&#8217;re using PHP ― add a simple&nbsp;switch:</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;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ie6'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now you can simply add &#8220;?ie6&#8221; to the URL in IE8 to see what your page looks like in IE6. You can even keep several tabs open to make sure it works with all&nbsp;versions.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/10/using-internet-explorer-8-for-ie6-and-ie7-testing/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Object Oriented CSS</title>
		<link>http://elbertf.com/2009/05/object-oriented-css/</link>
		<comments>http://elbertf.com/2009/05/object-oriented-css/#comments</comments>
		<pubDate>Sat, 16 May 2009 03:56:20 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=186</guid>
		<description><![CDATA[Okay, there is no such thing as object oriented CSS. But there are advantages in OO programming that we can use in&#160;CSS. 1 2 3 4 5 6 7 p &#123; margin: 0; &#125; &#160; #content p &#123; margin: 1em 0; &#125; In this example p would be a class and #content p would be [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, there is no such thing as object oriented CSS. But there are advantages in <a href="http://en.wikipedia.org/wiki/Object-oriented">OO programming</a> that we can use in&nbsp;CSS.</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="css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> p <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>In this example <code>p</code> would be a <em>class</em> and <code>#content p</code> would be an <em>instance</em> of that class. Let&#8217;s say the margin set in <code>p</code> is a <em>method</em> and we got the basic concepts of&nbsp;OOP.</p>
<p><span id="more-186"></span></p>
<h4>Practicle&nbsp;example</h4>
<p>A single HTML element can serve different purposes on a page. A list element for example can be used as a navigational menu (styled horizontally) and as a regular list in content (styled vertically). Sometimes it&#8217;s difficult to change one without changing the&nbsp;other.</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="css" style="font-family:monospace;">li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span> <span style="color: #993333;">inside</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* We only want bullets if a list appears in content */</span>
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">outside</span> <span style="color: #993333;">disc</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Here I created two <em>instances</em> of the <code>li</code> <em>object</em>. If you change either one it won&#8217;t effect the&nbsp;other.</p>
<h4>CSS&nbsp;Reset</h4>
<p>It&#8217;s useful to reset all style properties first using a CSS reset. In the above example I simply applied <code>list-style: none inside;</code> to the <code>li</code> element so you won&#8217;t see bullets (&bull;) unless you specify it. It&#8217;s easier to just reset everything at once, like&nbsp;this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inherit</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Trebuchet MS'</span><span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.4em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span> <span style="color: #993333;">inside</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inherit</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Adding this code on top of your CSS file will give you full control over your&nbsp;code.</p>
<p>As a bonus this will probably solve a lot of your cross-browser compatibility problems. This is because every browser applies <a href="http://lists.w3.org/Archives/Public/www-style/2008Jul/att-0124/defaultstyles.htm">different default CSS</a> to&nbsp;elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/object-oriented-css/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>

