<?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>gigaspartan</title>
	<atom:link href="http://gigaspartan.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gigaspartan.com</link>
	<description>You source for web news and answers.</description>
	<lastBuildDate>Thu, 03 Feb 2011 06:32:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Save The Kittens! PHP Must Follows!</title>
		<link>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/</link>
		<comments>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 06:13:09 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[Posts]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=173</guid>
		<description><![CDATA[There’s an old adage – dating back to the 1700s – which, in English, says: “A kitten dies each time a PHP programmer doesn’t follow best practices.” Okay, not really; but just go with it! Getting started in PHP can be a daunting experience. With that in mind, these 20 tips will teach you how [...]]]></description>
			<content:encoded><![CDATA[<p>There’s an old adage – dating back to the 1700s – which, in English, says: “<em>A kitten dies each time a PHP programmer doesn’t follow best practices.</em>” Okay, not really; but just go with it! </p>
<p> Getting started in PHP can be a daunting experience. With that in mind, these 20 tips will teach you how to follow best practices, and save lives…kitty lives. </p>
<p><span id="more-17889"></span></p>
<p></p>
<h2><span>0.</span> Program as Often as You Possibly Can</h2>
<blockquote class="pullquote">
<p> Programming often and with purpose will make the lessons you learn stick.</p>
</blockquote>
<p> Did you study a foreign language in school? Studied all the parts of speech, learned the verbs and how to conjugate them, followed along as your teacher said common phrases? </p>
<p> How much of that language do you still speak? </p>
<p> If you’re answer is, “<em>none</em>,” I’m willing to bet it’s due to the fact that you never actually <em>used</em> the language — you only studied it. But if you can still hold up a conversation, it’s likely because you actually spent some time speaking that language outside of the learning environment. Perhaps you spent a year abroad, or worked a job where a second language was necessary? </p>
<p> Whatever the reason, you retained it because you used it in real-life situations and <strong>put it into a personal context that is much easier to recall later.</strong> </p>
<blockquote>
<p> PHP is a foreign language, just like Spanish or French. In order to become comfortable with it, you need to use it outside of the classroom setting. Tutorials and sample projects are great for teaching the fundamentals, but unless you’re applying those concepts to your own projects, it will be much more difficult to apply those fundamentals in context and burn them into your memory. </p>
</blockquote>
<p> So, don’t worry that you “don’t know enough” to build a project. When you choose your project, you have a valid reason to research and implement a concept. Programming often and with purpose will make the lessons you learn stick. </p>
<p><span id="more-173"></span><br />
</p>
<h2><span>1.</span> Get Familiar with the PHP Manual</h2>
<p> Every list of tips for beginners has this tip, and for good reason.
</p>
<blockquote><p>Learning to navigate the PHP documentation is the single most useful thing you can do for yourself as a programmer. </p>
</blockquote>
<p> If you look in my browser history at the sites I most often visit, <a href="http://www.php.net/manual/en/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','www.php.net']);">the PHP manual</a> will be right at the top. I don’t suspect that will change for as long as PHP remains my programming language of choice. </p>
<p> At first, the manual does look rather daunting — it doesn’t seem to be particularly easy to browse, and the navigation can be a bit awkward at times. However, you’ll get the hang of it quickly. </p>
<p> Perhaps the best thing to know about the PHP manual is that <strong>most functions can be looked up using the pattern <code>http://php.net/<em>function-name</em></code></strong> in your address bar. For example, to look up the <a href="http://php.net/strpos" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>strpos()</code></a> function, use http://php.net/strpos, and for <a href="http://php.net/array-key-exists" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>array_key_exists()</code></a>, use http://php.net/array-key-exists. (NOTE: pay attention to the omission of parentheses and the substitution of hyphens (<code>-</code>) for the underscore (<code>_</code>) in the address.) </p>
<h3>1a. Read the Comments!</h3>
<p> It&#8217;s easy to overlook the comments, but do yourself a favor and have a look through them. If you&#8217;re getting an unexpected result from a function, <strong>chances are someone has spotted it and explained it in the comments.</strong> </p>
<p> You can also pick up a plethora of great tips and ideas from the developer community by reading through comments. </p>
<p></p>
<h2><span>2.</span> Take Advantage of the Huge Online PHP Community</h2>
<p> In addition to the PHP manual, there are wonderful developer communities all over the internet. Some of my personal favorites include <a href="http://stackoverflow.com/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','stackoverflow.com']);">StackOverflow.com</a> and the <a href="http://w3schools.invisionzone.com/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','w3schools.invisionzone.com']);">W3Schools.com forum</a>. </p>
<p> Additionally, Twitter is a surprisingly excellent place to post PHP questions. If you tag a tweet with <a href="http://twitter.com/search#search?q=%23php" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','twitter.com']);">#PHP</a>, it&#8217;s likely someone in the community will spot it and lend a hand. </p>
<p> <em>A Note About Twitter:</em> Of course, anything that&#8217;s useful will inevitably be overrun with spammers and those sorry individuals who deeply misunderstood the purpose of social media. If you&#8217;re going to use Twitter as a support network, you&#8217;ll probably want to routinely block or hide the accounts which spew job postings or retweet everything that mentions PHP. </p>
<blockquote>
<p> Just remember: as you get better, please try to <strong>pay it forward.</strong> The development community needs everyone to pitch in, and it won&#8217;t be long before you&#8217;ll have the ability to answer questions for other beginners. Don&#8217;t turn a deaf ear. </p>
</blockquote>
<p></p>
<h2><span>3.</span> Don&#8217;t Put Off Best Practices for Later</h2>
<p> As you&#8217;re learning, you&#8217;re going to hear a lot about &#8220;best practices&#8221; in programming; stuff like prepared statements and PEAR coding standards. </p>
<p> Do not put off learning this stuff because it seems hard.</p>
<blockquote>
<p> If something is a best practice, it&#8217;s not because we (meaning other PHP developers) got together and said, &#8220;How can we make life harder for the noobs?&#8221; </p>
</blockquote>
<p> Best practices exist to keep your scripts secure, fast, and manageable. Learn them as early as you can. In fact, don&#8217;t even bother learning the wrong way. </p>
<p> It takes just about the exact same amount of learning to figure out <code>mysql_query()</code> as it does to learn <a href="http://php.net/pdo" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">PDO</a> or <a href="http://php.net/mysqli" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">MySQLi</a>. So if you start with your choice of the latter two, you&#8217;re starting with a strong foundation in database interaction and, really, you&#8217;ve put in less overall effort. </p>
<p></p>
<h2><span>4.</span> Don&#8217;t Put Off Best Practices for Later!</h2>
<p> I just wanted to make sure you saw this. </p>
<blockquote>
<p> Seriously, folks. <strong>Don&#8217;t take shortcuts.</strong> Every time you violate best practices because the right way seems &#8220;too hard,&#8221; BP dips a kitten in crude oil. </p>
</blockquote>
<p> So if you won&#8217;t do it for yourself, your projects, your peers, or the advancement of the community at large, <strong>at least consider the kittens.</strong> </p>
<p></p>
<h2><span>5.</span> Make Code Self-Documenting</h2>
<blockquote class="pullquote">
<p> If you need to squeeze characters off your variable names to shave .2ms off your script’s execution time, there’s likely a whole different problem going on.</p>
</blockquote>
<p> It&#8217;s tempting, early on, to be &#8220;clever&#8221; with your variable and function names. Maybe you read an article about performance, or saw a code snippet that accomplished a ton of work in two lines of code. Maybe you want to create your own &#8220;signature style&#8221; of coding. Maybe you just heard that I hate it and you wanted to piss me off. </p>
<p> Whatever your temptation, <strong>resist it at all costs.</strong> </p>
<p> Consider the following snippet of code: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
 <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> b<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jason.lengstorf@copterlabs.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The email address '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' belongs to the domain '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$d</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">function</span> b<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> Does that make any sense to you? </p>
<p> Of course, you <em>can</em> figure out what it does, but why force anyone trying to work in your code to spend the extra 1-5 minutes scratching his head, trying to remember what $c is storing. </p>
<p> So let&#8217;s take that code and make it self-documenting: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
 <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> sanitize_string<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'support@google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$email_pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$domain</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email_pieces</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The email address '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' belongs to the domain '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domain</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">function</span> sanitize_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> There. Much better. Now, just by glancing at the code, you can get the general idea of what&#8217;s going on. No head-scratching, no muttered curses, and most importantly, <strong>no real difference.</strong> </p>
<p> Sure, you save a few bytes with short variable names. But, honestly, if you need to squeeze characters off your variable names to shave .2ms off your script&#8217;s execution time, there&#8217;s likely a whole different problem going on. </p>
<p></p>
<h2><span>6.</span> Add a Comment to Anything You Had to Think About</h2>
<blockquote class="pullquote">
<p>Comments are the sign of a competent programmer.</p>
</blockquote>
<p> Comments are not the sign of a novice. In fact, as I see more and more code that&#8217;s not mine, I&#8217;m starting to think that comments are the sign of a competent programmer, as they seem to be the only ones doing it. </p>
<p> If your code is self-documenting, you won&#8217;t require too many comments. However, no matter how clear your variable and function names are, you&#8217;ll always have spots where the action taken simply isn&#8217;t that obvious.</p>
<p> When that happens, slap a comment in there. &#8220;Future You&#8221; will give &#8220;Present You&#8221; a high five when the time comes to update the script. </p>
<blockquote>
<p> As a rule of thumb, <em>if you had to stop and think for a few seconds about what needed to happen to make the script work properly, it&#8217;s probably a good spot for a quick note.</em> </p>
</blockquote>
<p> Consider the following: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17315"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p173code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> What does that do? Did you have to stop and think about it? Do you still not know for sure what&#8217;s stored in $extension? </p>
<p> Look at that snippet again, but with one quick comment: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17316"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p173code16"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get the extension off the image filename</span>
<span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Now, even if you don&#8217;t know <em>how or why that code works,</em> you at least know that <code>$extension</code> refers specifically to an image extension. If that saves &#8220;Future You&#8221; or another developer five seconds of processing the script&#8217;s intent, <strong>it was well worth your ten seconds of effort to add the comment in the first place.</strong> </p>
<p> As with most things, moderation is key. Too few comments and you risk leaving the next developer (or Future You) puzzled by a code snippet, which can even lead to accidental breaking of code because the solution, without explanation, might look silly or superfluous. Too many and it becomes too difficult to scan through your code, which is equally frustrating. </p>
<blockquote><p>Moderation is key. </p>
</blockquote>
<p></p>
<h2><span>7.</span> Learn Docblock and Use It</h2>
<p> If I could be sure every developer in the world would do one thing with absolute consistency, I think it would be the use of the <a href="http://pear.php.net/manual/en/standards.sample.php">Docblock commenting standard</a>. </p>
<p> I have a few reasons for my strong support of Docblock: </p>
<ol>
<li> It requires one to think about the <em>what</em> and <em>why</em> for each file, function, method, and so on. </li>
<li> It gives a clear description of the expected types for parameters and return values in functions/methods </li>
<li> It provides a quick description of what the code does </li>
<li> When coupled with one of the many IDEs that support Docblock, it creates code hinting (which allows you to see a description and expected parameters/return values for the method or function you&#8217;re using) </li>
</ol>
<p> This tip does border on the upper level of beginner, but I group this under &#8220;best practices to be learned as quickly as possible.&#8221; Feel free to skip it, but before you do, <strong>think about the kittens.</strong> </p>
<p> Docblock shows its versatility best when used to document a class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p173code17"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * A simple class to get the sum or difference of $_foo and a value
 *
 * @author Pat Herlihy
 * @copyright 2011 Pat Herlihy
 * @license http://www.opensource.org/licenses/mit-license.html
*/</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Pat_Test
<span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * The value to use in addition and subtraction
  * @var int
 */</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_foo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * Adds a value to $_foo and returns the sum *
  * @param int $add_me The value to add to $_foo
  * @return int The sum of $_foo and $add_me
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> add_to_foo<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$add_me</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_foo <span style="color: #339933;">+=</span> <span style="color: #000088;">$add_me</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * Subtracts a value from $_foo and returns the difference
  * @param int $subtract_me The value to subtract from $_foo
  * @return int The difference of $_foo and $subtract_me
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> subtract_from_foo<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$subtract_me</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_foo <span style="color: #339933;">-=</span> <span style="color: #000088;">$subtract_me</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> At first it might look overwhelming, but <strong>the benefits are very much worth taking the time to familiarize yourself with the syntax.</strong> </p>
<p></p>
<h2><span>8.</span> Don&#8217;t Be Too Hardcore to Use an IDE</h2>
<p> If you don&#8217;t already know the type, you will soon: the developers who think <a href="http://en.wikipedia.org/wiki/Real_Programmer" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','en.wikipedia.org']);">real programmers don&#8217;t use IDEs.</a> </p>
<p> Now, look: if you want to impress people, learn to juggle. Refusing to use anything but <a href="http://www.gnu.org/software/emacs/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','www.gnu.org']);">Emacs</a> in the command line to write scripts will not get you chicks or grant you instant hacker status; it <em>will</em>, however, hang a  sign on your forehead warning your coworkers that you are, in fact, <strong>That Guy.</strong> </p>
<p> <strong><em>Don&#8217;t be That Guy.</em></strong> </p>
<blockquote>
<p> There is nothing wrong with using software to give you on-the-fly syntax highlighting, error-checking, and code hints. </p>
</blockquote>
<p> How in-depth your IDE goes is entirely up to you. Personally, I really like <a href="http://netbeans.org/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','netbeans.org']);">Netbeans</a>. I&#8217;ve heard tons of praise for <a href="http://panic.com/coda/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','panic.com']);">Coda</a> for Mac (though it&#8217;s not really an IDE), and I previously used <a href="http://eclipse.org/" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','eclipse.org']);">Eclipse</a> before moving to Netbeans.  </p>
<p> Whatever IDE you choose, <strong>you&#8217;ll see your coding speed increase and your facepalm-worthy bugs decrease.</strong> Further, as you expand your code library, you&#8217;ll have code hinting for all of your custom software. (Because you&#8217;re using Docblock, right? Right?!) </p>
<p> Don&#8217;t think IDEs are uncool &#8212; no matter what &#8220;That Guy&#8221; tries to tell you. </p>
<p></p>
<h2><span>9.</span> Group Common Code Into Functions</h2>
<blockquote class="pullquote">
<p>If you see an action repeated, it&#8217;s time to strongly consider moving that code into a function.</p>
</blockquote>
<p> When you first start programming, it&#8217;s easy to start at the top of the page and work down, adding each piece of code right where it&#8217;s needed. </p>
<p> However, when you code this way, you&#8217;ll begin to notice that certain pieces of code are appearing over and over again. This is a minefield when it comes to maintenance and upgrades, because you have to hunt through each file for every occurrence of that action to change its functionality. </p>
<p> If you see an action repeated, even if it&#8217;s only twice, it&#8217;s time to strongly consider moving that code into a function. </p>
<p> Consider the following for example: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code18"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$unclean1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;a href=&quot;javascript:alert(\'Holy Crap!\');&quot;&gt;Click Me!&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$detagged1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$deslashed1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed1</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>
&nbsp;
<span style="color: #000088;">$unclean2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Let's call Björn!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$detagged2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$deslashed2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed2</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>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clean2</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> As you can see, both of those strings required a few steps before they could be considered safe to use. However, you&#8217;ll also notice that those same steps could be considered necessary for <em>every bit of information that is passed to the script.</em> </p>
<p> This is an instance where using a function instead is far more desirable: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code19'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17319"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p173code19"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$unclean1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;a href=&quot;javascript:alert(\'Holy Crap!\');&quot;&gt;Click Me!&lt;/a&gt;'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$unclean2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Let's call Björn!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$clean1</span> <span style="color: #339933;">=</span> sanitize_input<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clean2</span> <span style="color: #339933;">=</span> sanitize_input<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unclean2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$clean1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clean2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> sanitize_input<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$input</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$detagged</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$deslashed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/stripslashes"><span style="color: #990000;">stripslashes</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detagged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$deslashed</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p> By wrapping the common code in a function, it&#8217;s a bit easier to see what&#8217;s going on, and it&#8217;s much easier to edit the steps you want to take when sanitizing your input. </p>
<p></p>
<h2><span>10.</span> Group Related Functions Into Classes</h2>
<p> Getting a handle on OOP is another one of those things that I file under &#8220;best practices to learn as quickly as possible.&#8221; </p>
<blockquote>
<p> If you have a handful of functions that all deal with database actions, you can save yourself a lot of time and effort by grouping them into classes. </p>
</blockquote>
<p><a href="http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/">Learning object-oriented programming</a> is definitely outside the scope of this list, but I felt it was definitely worth mentioning in this beginners&#8217; list. </p>
<p></p>
<h2><span>11.</span> Use Constants, Not Globals</h2>
<blockquote class="pullquote">
<p>PHP allows you to define your own constants with the function <a href="http://php.net/define" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>define().</code></a></p>
</blockquote>
<p> When I first started programming on larger projects, I found myself using <a href="http://php.net/global" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);">global variables</a> more often than seemed necessary or reasonable. Admitting you have a problem is the first step.</p>
<p> I was storing things like application-wide data (such as the site&#8217;s name or the maximum image width) and database credentials in variables, and I found myself required to use the <a href="http://php.net/manual/en/reserved.variables.globals.php" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>$GLOBALS</code></a> superglobal to access this information. </p>
<p> Then I realized <strong>PHP allows you to define your own constants</strong> with the function <a href="http://php.net/define" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>define()</code></a>. </p>
<p> A constant is a great way to store information like the aforementioned app-wide data and database info. An additional bonus is that it can&#8217;t be modified, so you can&#8217;t accidentally overwrite your database password somewhere later in the script. </p>
<p> As a matter of best practices, the use of globals is generally discouraged to begin with, so the use of constants is preferred anyways. Review the following code for an example and see for yourself: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17320"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p173code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FOO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'constant value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'global value'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> baz<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> baz<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$constant</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' Constant: '</span> <span style="color: #339933;">.</span> FOO<span style="color: #339933;">;</span>
   <span style="color: #000088;">$global</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Global: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$constant</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2><span>12.</span> Don&#8217;t Be Afraid to Use Includes</h2>
<p> Often, as you&#8217;re building larger products, it will make a lot of sense to break it apart into smaller chunks, or include files. </p>
<p> A generally accepted way to look at includes is to <strong>put any bit of code that will be used in multiple scripts into an include file</strong> (such as your database connection details, the header and footer data that is common across the whole site, utility functions like your input sanitization actions, etc.) so it can be pulled in by the file that needs it, rather than being copy-pasted. </p>
<p> For example, on a site with multiple pages, a standard template may emerge that looks something like this: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17321"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p173code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Application-wide data and database connection</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'constants.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'database.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Utility functions</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'utilities.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Header markup</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'header.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* * Page-specific processing goes here */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Footer markup</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'footer.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2><span>13.</span> Don&#8217;t Obsess Over Performance</h2>
<p> This is a point of near-paralysis for some developers, and it&#8217;s really too bad; there is a blurry line between writing efficient code and wasting time trying to squeeze an extra 5ms out of a script&#8217;s execution. </p>
<p> Definitely read a few performance articles and learn some of the major pitfalls that can drag your scripts to a slow crawl, but don&#8217;t waste extra time refactoring your code to change double quotes to single quotes because you found out it was a tiny fraction faster. </p>
<blockquote>
<p> Use your head, avoid the big problems, and keep your ears open, in case a tip you&#8217;ve never heard comes along to speed up your code, but don&#8217;t make it a race. </p>
</blockquote>
<p>No one can tell the difference between a 25ms page load and a 40ms page load. Make sure it&#8217;s not 700ms and move on to more important things. </p>
<p></p>
<h2><span>14.</span> Avoid Marrying HTML to Your Scripts</h2>
<p> This can be tricky, but do your best to avoid tangling up your HTML markup in your PHP. It&#8217;s nearly impossible to get away from it completely, but try to <strong>make sure that you don&#8217;t include any non-essential HTML markup in your code.</strong> </p>
<p> Consider the following: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17322"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p173code22"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;example-div&quot;&gt;&lt;p&gt;This is some test content.&lt;/p&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Was it necessary for that code to wrap the paragraph tag in a div? Could it have been modified to only include the paragraph tag that holds the text? Have a look at an alternative solution: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17323"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p173code23"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;example-div&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;p&gt;This is some test content.&lt;/p&gt;'</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p> <em>Note:</em> This example is grossly oversimplified. Generating HTML with PHP is usually more of an issue when dealing with a complex function or method that organizes a dataset. The point I&#8217;m trying to make is that it can sometimes be tempting to include more markup than is necessary in the output. </p>
<p> In most cases you can keep the HTML outside the PHP, which makes things easier to read and, usually, easier to work with as well. </p>
<p></p>
<h2><span>15.</span> Try to Use at Least One Unfamiliar Concept in Every Project</h2>
<blockquote class="pullquote">
<p>Push yourself outside your comfort zone.</p>
</blockquote>
<p> You&#8217;re never going to learn if you keep doing the same old thing. Try out a concept that you&#8217;re not quite comfortable with on every project you possibly can. </p>
<p>Don&#8217;t be over-ambitious, but definitely push yourself outside your comfort zone. It gives you a challenge, saves you from getting bored doing the same old thing over and over again, and forces you to progress as a developer. </p>
<p> Look at the project, find all the bits that you know well (or at least well enough), and then pick an area you&#8217;d like to understand. Sign up for it. Then do it. </p>
<p></p>
<h2><span>16.</span> Don&#8217;t Be Too Proud to Change</h2>
<p> You will be wrong. Frequently. But that&#8217;s not a bad thing. </p>
<p> As you improve, you&#8217;ll find newer, better solutions to problems that you&#8217;ve faced in the past. Don&#8217;t feel stupid;  you&#8217;re learning. </p>
<p> But it&#8217;s extremely important that you don&#8217;t become attached to the code you write. Don&#8217;t think your way is better just because it&#8217;s your way. If you happen across a great solution that makes yours look like a Band-Aid on a bullet wound, use it! Pay attention to what&#8217;s different and what you did that could have been better. File that away under, &#8220;Things I&#8217;ve Learned.&#8221; </p>
<blockquote>
<p> Never allow yourself to believe that an inelegant solution is acceptable because it&#8217;s yours. That&#8217;s hubris (which, if you&#8217;re not aware, doesn&#8217;t generally result in happy fun times). </p>
</blockquote>
<p></p>
<h2><span>17.</span> Validate</h2>
<p> If you&#8217;re a web programmer, start becoming familiar with <strong>input validation</strong> as soon as possible. </p>
<p> Keep in mind: <em>validation</em> is quite different from <em>sanitization</em>. </p>
<blockquote>
<p> Input validation is the practice of making sure data matches the format you&#8217;ve requested, like checking an email field for a valid email address or ensuring that a submitted username is 8-20 alphanumeric characters. </p>
</blockquote>
<p> It can be tedious and a pain in the ass, but making sure that only valid data makes it through your processing scripts will enhance the user experience and avoid a lot of bugs in the scripts that have to use the data later on.</p>
<p></p>
<h2><span>18.</span> Whitelists Instead of Blacklists</h2>
<blockquote class="pullquote">
<p> If you&#8217;re not on top of your blacklist, vulnerabilities appear. </p>
</blockquote>
<p> In plenty of situations, you&#8217;ll want to block or get rid of certain tags, words, email addresses, or other various bits of data. </p>
<p> A common solution is to use a <code>blacklist</code>: a collection of the tags, terms, etc. that aren&#8217;t allowed. </p>
<p> This poses a problem, however; you have to be more clever than the person trying to do something naughty. For instance, in the case of disabling JavaScript in posts, you might blacklist the <code>onclick</code> attribute, as well as most of the event attributes, but what if you forget one? What if a new one is added to the spec sometime in the future? </p>
<p> If you&#8217;re not on top of your blacklist, vulnerabilities appear. </p>
<p> However, to save headache later, use a <code>whitelist</code> whenever possible. A whitelist is the opposite of a blacklist: a collection of allowed tags, terms, etc. </p>
<p> For instance, in the <a href="http://php.net/strip-tags" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','php.net']);"><code>strip_tags()</code></a> function, you can provide a whitelist to specify which tags are allowed in strings: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p173code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17324"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p173code24"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/strip_tags"><span style="color: #990000;">strip_tags</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;em&gt;&lt;strong&gt;&lt;tt&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p> Now your problem is most likely going to be that you can actually do <em>less</em> than you wanted, but that&#8217;s far safer and usually less of an emergency to handle. </p>
<p> You can&#8217;t get away with it in every situation, but saying what <em>is</em> allowed vs. what <em>isn&#8217;t</em> will provide you with more confidence and control over your scripts. </p>
<p></p>
<h2><span>19.</span> Learn to Count Like a Computer</h2>
<p> Are you looking for tip #20? Remember that in nearly all cases, <strong>counts in PHP start at 0,</strong> so this is actually the 20th tip.  You&#8217;ll find this to be the case in most languages; don&#8217;t let this one trip you up!</p>
<p></p>
<h2>Summary</h2>
<p> If you&#8217;re a beginner, the tips covered above will help you take great strides toward good habits and best practices. Don&#8217;t get overwhelmed if all of this is news to you; take things one step at a time (see tip #15). </p>
<p> Do you have a PHP tip for beginners? An addendum to one of the tips above? A question? <strong>Let us know in the comments!</strong> </p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/02/01/save-the-kittens-php-must-follows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tutorial: Object-Oriented PHP for Beginners</title>
		<link>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/</link>
		<comments>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 03:28:09 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=157</guid>
		<description><![CDATA[For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. In this tutotial you’ll learn the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code. Understanding Object-Oriented Programming Object-oriented programming is a style [...]]]></description>
			<content:encoded><![CDATA[<p>
For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. In this tutotial you’ll learn the concepts behind <strong>object-oriented programming</strong> (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code.
</p>
<p></p>
<h2>Understanding Object-Oriented Programming</h2>
<p>
Object-oriented programming is a style of coding that allows developers to group similar tasks into <strong>classes</strong>. This helps keep code following the tenet <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">“don’t repeat yourself” (DRY)</a> and easy-to-maintain.
</p>
<blockquote class="pullquote pqRight">
<p>
“Object-oriented programming is a style of coding that allows developers to group similar tasks into <strong>classes</strong>.”
</p>
</blockquote>
<p><span id="more-157"></span></p>
<p>
One of the major benefits of DRY programming is that, if a piece of information changes in your program, usually <strong>only one change is required to update the code.</strong> One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely more frustrating game of <em>Where’s Waldo?</em> as they hunt for duplicated data and functionality.
</p>
<p>
OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming.
</p>
<p></p>
<h2>Understanding Objects and Classes</h2>
<p>
Before you can get too deep into the finer points of OOP, a basic understanding of the differences between <strong>objects</strong> and <strong>classes</strong> is necessary. This section will go over the building blocks of classes, their different capabilities, and some of their uses.
</p>
<h3>Recognizing the Differences Between Objects and Classes</h3>
<div class="tutorial_image">
    <img src="http://d2o0t5hpnwv4c1.cloudfront.net/734_oop/images/blueprint-houses.jpg" border="0">
</p>
<p>
        Photos by <a href="http://www.flickr.com/photos/instantjefferson/">Instant Jefferson</a> and <a href="http://www.flickr.com/photos/johnwardell/">John Wardell</a>
    </p>
</div>
<blockquote class="pullquote pqRight">
<p>
“Developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however.”
</p>
</blockquote>
<p>
Right off the bat, there’s confusion in OOP: seasoned developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however, though the difference can be tough to wrap your head around at first.
</p>
<p>
A class, for example, is like <strong>a blueprint for a house</strong>. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn’t exist.
</p>
<p>
An object, then, is like <strong>the actual house</strong> built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it’s just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.
</p>
<p>
<strong>Classes form the structure of data and actions and use that information to build objects.</strong> More than one object can be built from the same class at the same time, each one independent of the others. Continuing with our construction analogy, it’s similar to the way an entire subdivision can be built from the same blueprint: 150 different houses that all look the same but have different<br />
families and decorations inside.
</p>
<h3>Structuring Classes</h3>
<p>
The syntax to create a class is pretty straightforward: declare a class using the <tt>class</tt> keyword, followed by the name of the class and a set of curly braces (<tt>{}</tt>):
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code70'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15770"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p157code70"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
After creating the class, a new class can be instantiated and stored in a variable using the <tt>new</tt> keyword:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code71'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15771"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code71"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
To see the contents of the class, use <tt>var_dump()</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code72'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15772"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code72"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Try out this process by putting all the preceding code in a new file called <tt>test.php</tt> in [your local] testing folder:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code73'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15773"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code73"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Load the page in your browser at <tt>http://localhost/test.php</tt> and the following should display:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code74'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15774"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code74"><pre class="php" style="font-family:monospace;">object<span style="color: #009900;">&#40;</span>MyClass<span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">#1 (0) { }</span></pre></td></tr></table></div>

<p>
<strong>In its simplest form, you’ve just completed your first OOP script.</strong>
</p>
<p></p>
<h2>Defining Class Properties</h2>
<p>
To add data to a class, <strong>properties</strong>, or class-specific variables, are used. These work exactly like regular variables, except they’re bound to the object and therefore can only be accessed using the object.
</p>
<p>
To add a property to <tt>MyClass</tt>, add the following code to your script:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code75'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15775"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code75"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
The keyword <tt>public</tt> determines the visibility of the property, which you’ll learn about a little later in this chapter. Next, the property is named using standard variable syntax, and a value is assigned (though class properties do not need an initial value).
</p>
<p>
To read this property and output it to the browser, reference the object from which to read and the property to be read:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code76'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15776"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code76"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Because multiple instances of a class can exist, if the individual object is not referenced, the script would be unable to determine which object to read from. The use of the arrow (<tt>-></tt>) is an OOP construct that accesses the contained properties and methods of a given object.
</p>
<p>
Modify the script in <tt>test.php</tt> to read out the property rather than dumping the whole class by modifying the code as shown:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code77'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15777"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p157code77"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output the property</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reloading your browser now outputs the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code78'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15778"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p157code78"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!</span></pre></td></tr></table></div>

<p></p>
<h2>Defining Class Methods</h2>
<p>
<strong>Methods</strong> are class-specific functions. Individual actions that an object will be able to perform are defined within the class as methods.
</p>
<p>
For instance, to create methods that would set and get the value of the class property <tt>$prop1</tt>, add the following to your code:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code79'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15779"><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
</pre></td><td class="code" id="p157code79"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — OOP allows objects to reference themselves using <tt>$this</tt>. When working within a method, use <tt>$this</tt> in the same way you would use the object name outside the class.
</p>
<p>
To use these methods, call them just like regular functions, but first, reference the object they belong to. Read the property from <tt>MyClass</tt>, change its value, and read it out again by making the modifications below:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code80'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15780"><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
</pre></td><td class="code" id="p157code80"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Get the property value</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Set a new one</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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;">// Read it out again to show the change</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reload your browser, and you’ll see the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code81'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15781"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p157code81"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">new</span> property value<span style="color: #339933;">!</span></pre></td></tr></table></div>

<blockquote>
<p>
“The power of OOP becomes apparent when using multiple instances of the<br />
same class.”
</p>
</blockquote>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code82'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15782"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p157code82"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create two objects</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1 from both objects</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set new values for both objects</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I belong to the second instance!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output both objects' $prop1 value</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
When you load the results in your browser, they read as follows:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code83'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15783"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code83"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">class</span> property<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a new property value!
I belong to the second instance!</span></pre></td></tr></table></div>

<p>
As you can see, <strong>OOP keeps objects as separate entities</strong>, which makes for easy separation of different pieces of code into small, related bundles.
</p>
<p></p>
<h2>Magic Methods in OOP</h2>
<p>
To make the use of objects easier, PHP also provides a number of <strong>magic methods</strong>, or special methods that are called when certain common actions occur within objects. This allows developers to perform a number of useful tasks with relative ease.
</p>
<h3>Using Constructors and Destructors</h3>
<p>
When an object is instantiated, it’s often desirable to set a few things right off the bat. To handle this, PHP provides the magic method <tt>__construct()</tt>, which is called automatically whenever a new object is<br />
created.
</p>
<p>
For the purpose of illustrating the concept of constructors, add a constructor to <tt>MyClass</tt> that will output a message whenever a new instance of the class is created:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code84'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15784"><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" id="p157code84"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — <tt>__CLASS__</tt> returns the name of the class in which it is called; this is what is known as a <a href="http://us3.php.net/manual/en/language.constants.predefined.php">magic constant</a>. There are several available magic constants, which you can read more about in the PHP manual.
</p>
<p>
Reloading the file in your browser will produce the following result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code85'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15785"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code85"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.</span></pre></td></tr></table></div>

<p>
To call a function when the object is destroyed, the <tt>__destruct()</tt> magic method is available. This is useful for class cleanup (closing a database connection, for instance).
</p>
<p>
Output a message when the object is destroyed by defining the magic method<br />
<tt>__destruct()</tt> in <tt>MyClass</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code86'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15786"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p157code86"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
With a destructor defined, reloading the test file results in the following output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code87'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15787"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code87"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<blockquote>
<p>
“When the end of a file is reached, PHP automatically releases all resources.”
</p>
</blockquote>
<p>
To explicitly trigger the destructor, you can destroy the object using the<br />
function <tt>unset()</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code88'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15788"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p157code88"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Now the result changes to the following when loaded in your browser:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code89'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15789"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code89"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></td></tr></table></div>

<h3>Converting to a String</h3>
<p>
To avoid an error if a script attempts to output <tt>MyClass</tt> as a string, another magic method is used called <tt>__toString()</tt>.
</p>
<p>
Without <tt>__toString()</tt>, <em>attempting to output the object as a string results in a fatal error</em>. Attempt to use <tt>echo</tt> to output the object without a magic method in place:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code90'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15790"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p157code90"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This results in the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code91'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15791"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code91"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
&nbsp;
Catchable fatal error<span style="color: #339933;">:</span> Object of <span style="color: #000000; font-weight: bold;">class</span> MyClass could not be converted to string in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">40</span></pre></td></tr></table></div>

<p>
To avoid this error, add a <tt>__toString()</tt> method:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code92'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15792"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code" id="p157code92"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
In this case, attempting to convert the object to a string results in a call to the <tt>getProperty()</tt> method. Load the test script in your browser to see the result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code93'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15793"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code93"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
Using the toString method<span style="color: #339933;">:</span> I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></td></tr></table></div>

<p>
<strong>Tip</strong> — In addition to the magic methods discussed in this section, several others are available. For a complete list of magic methods, see the  <a href="http://us2.php.net/manual/en/language.oop5.magic.php">PHP manual page</a>.
</p>
<p></p>
<h2>Using Class Inheritance</h2>
<p>
<strong>Classes can inherit the methods and properties of another class</strong> using the <tt>extends</tt> keyword. For instance, to create a second class that extends <tt>MyClass</tt> and adds a method, you would add the following to your test file:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code94'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15794"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code" id="p157code94"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Upon reloading the test file in your browser, the following is output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code95'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15795"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code95"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Overwriting Inherited Properties and Methods</h3>
<p>
To change the behavior of an existing property or method in the new class, you can simply overwrite it by declaring it again in the new class:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code96'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15796"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code" id="p157code96"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This changes the output in the browser to:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code97'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15797"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code97"><pre class="php" style="font-family:monospace;">A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Preserving Original Method Functionality While Overwriting Methods</h3>
<p>
To add new functionality to an inherited method while keeping the original method intact, use the <tt>parent</tt> keyword with the <strong>scope resolution operator</strong> (<tt>::</tt>):
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code98'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15798"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p157code98"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Call the parent class's constructor</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This outputs the result of both the parent constructor and the new class’s constructor:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code99'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15799"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p157code99"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<p></p>
<h2>Assigning the Visibility of Properties and Methods</h2>
<p>
For added control over objects, methods and properties are assigned visibility. This controls how and from where properties and methods can be accessed. There are three visibility keywords: <tt>public</tt>, <tt>protected</tt>, and <tt>private</tt>. In addition to its visibility, a method or property can be declared as <tt>static</tt>, which allows them to be accessed without an instantiation of the class.
</p>
<blockquote class="pullquote pqRight"><p>“For added control over objects, methods and properties are assigned visibility.”</p>
</blockquote>
<p>
<strong>Note</strong> — Visibility is a new feature as of PHP 5. For information on <a href="http://us2.php.net/manual/en/language.oop5.php">OOP compatibility with PHP 4</a>, see the PHP manual page.
</p>
<h3>Public Properties and Methods</h3>
<p>
All the methods and properties you’ve used so far have been public. This means that they can be accessed anywhere, both within the class and externally.
</p>
<h3>Protected Properties and Methods</h3>
<p>
When a property or method is declared <tt>protected</tt>, <strong>it can only be accessed within the class itself or in descendant classes</strong> (classes that extend the class containing the protected method).
</p>
<p>
Declare the <tt>getProperty()</tt> method as protected in <tt>MyClass</tt> and try to access it directly from outside the class:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code100'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157100"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code" id="p157code100"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Attempt to call a protected method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Upon attempting to run this script, the following error shows up:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code101'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157101"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code101"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">protected</span> method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">''</span> in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">55</span></pre></td></tr></table></div>

<p>
Now, create a new method in <tt>MyOtherClass</tt> to call the <tt>getProperty()</tt> method:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code102'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157102"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code" id="p157code102"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Call the protected method from within a public method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This generates the desired result:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code103'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157103"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code103"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></td></tr></table></div>

<h3>Private Properties and Methods</h3>
<p>
A property or method declared <tt>private</tt> is accessible <strong>only from within the class that defines it</strong>. This means that <em>even if a new class extends the class that defines a private property,</em> that property or method will not be available at all within the child class.
</p>
<p>
To demonstrate this, declare <tt>getProperty()</tt> as private in <tt>MyClass</tt>, and attempt to call <tt>callProtected()</tt> from<br />
<tt>MyOtherClass</tt>:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code104'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157104"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code" id="p157code104"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Reload your browser, and the following error appears:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code105'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157105"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p157code105"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">private</span> method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">'MyOtherClass'</span> in <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">49</span></pre></td></tr></table></div>

<h3>Static Properties and Methods</h3>
<p>
A method or property declared <tt>static</tt> can be accessed without first instantiating the class; you simply supply the class name, scope resolution operator, and the property or method name.
</p>
<blockquote>
<p>
“One of the major benefits to using static properties is that they keep their stored values for the duration of the script.”
</p>
</blockquote>
<p>
To demonstrate this, add a static property called <tt>$count</tt> and a static method called <tt>plusOne()</tt> to <tt>MyClass</tt>. Then set up a <tt>do...while</tt> loop to output the incremented value of <tt>$count</tt> as long as the value is less than 10:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code106'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157106"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code" id="p157code106"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</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;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<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: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<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: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> plusOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;The count is &quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">++</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<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: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">do</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Call plusOne without instantiating MyClass</span>
    <span style="color: #b1b100;">echo</span> MyClass<span style="color: #339933;">::</span><span style="color: #004000;">plusOne</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> MyClass<span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Note</strong> — When accessing static properties, the dollar sign<br />
(<tt>$</tt>) comes <em>after the scope resolution operator.</em>
</p>
<p>
When you load this script in your browser, the following is output:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code107'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157107"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p157code107"><pre class="php" style="font-family:monospace;">The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">1</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">2</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">3</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">4</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">5</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">6</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">7</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">8</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">9</span><span style="color: #339933;">.</span>
The <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a> is <span style="color: #cc66cc;">10</span><span style="color: #339933;">.</span></pre></td></tr></table></div>

<p></p>
<h2>Commenting with DocBlocks</h2>
<blockquote class="pullquote pqRight">
<p>
“The DocBlock commenting style is a widely<br />
accepted method of documenting classes.”
</p>
</blockquote>
<p>
While not an official part of OOP, the <a href="http://en.wikipedia.org/wiki/PHPDoc">DocBlock</a> commenting style is a widely accepted method of documenting classes. Aside from providing a standard for<br />
developers to use when writing code, it has also been adopted by many of the most popular software development kits (SDKs), such as <a href="http://eclipse.org">Eclipse</a> and <a href="http://netbeans.org">NetBeans</a>, and will be used to generate code hints.
</p>
<p>
A DocBlock is defined by using a block comment that starts with an additional asterisk:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code108'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157108"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p157code108"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * This is a very basic DocBlock
 */</span></pre></td></tr></table></div>

<p>
The real power of DocBlocks comes with the ability to use <strong>tags</strong>, which start with an at symbol (<tt>@</tt>) immediately followed by the tag name and the value of the tag. <strong>DocBlock tags allow developers to define authors of a file, the license for a class, the property or method information, and other useful information.</strong>
</p>
<p>
The most common tags used follow:
</p>
<ul>
<li><strong>@author</strong>: The author of the current element (which might be a class, file, method, or any bit of code) are listed using this tag. Multiple author tags can be used in the same DocBlock if more than one author is credited. The format for the author name is <tt>John Doe <john.doe@email.com></tt>.</li>
<li><strong>@copyright</strong>: This signifies the copyright year and name of the copyright holder for the current element. The format is <tt>2010 Copyright Holder</tt>.</li>
<li><strong>@license</strong>: This links to the license for the current element. The format for the license information is<br />
        <tt>http://www.example.com/path/to/license.txt License Name</tt>.</li>
<li><strong>@var</strong>: This holds the type and description of a variable or class property. The format is <tt>type element description</tt>.</li>
<li><strong>@param</strong>: This tag shows the type and description of a function or method parameter. The format is <tt>type $element_name element description</tt>.</li>
<li><strong>@return</strong>: The type and description of the return value of a function or method are provided in this tag. The format is <tt>type return element description</tt>.</li>
</ul>
<p>
A sample class commented with DocBlocks might look like this:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code109'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157109"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code" id="p157code109"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A simple class
 *
 * This is the long description for this class,
 * which can span as many lines as needed. It is
 * not required, whereas the short description is
 * necessary.
 *
 * It can also span multiple paragraphs if the
 * description merits that much verbiage.
 *
 * @author Jason Lengstorf &lt;jason.lengstorf@ennuidesign.com&gt;
 * @copyright 2010 Ennui Design
 * @license http://www.php.net/license/3_01.txt PHP License 3.01
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> SimpleClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * A public variable
     *
     * @var string stores data for the class
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Sets $foo to a new value upon class instantiation
     *
     * @param string $val a value required for the class
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <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: #000088;">$val</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Multiplies two integers
     *
     * Accepts a pair of integers and returns the
     * product of the two.
     *
     * @param int $bat a number to be multiplied
     * @param int $baz a number to be multiplied
     * @return int the product of the two parameters
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bat</span><span style="color: #339933;">,</span> <span style="color: #000088;">$baz</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$bat</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$baz</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
Once you scan the preceding class, the benefits of DocBlock are apparent: everything is clearly defined so that the next developer can pick up the code and <em>never have to wonder what a snippet of code does or what it should contain.</em>
</p>
<p></p>
<h2>Comparing Object-Oriented and Procedural Code</h2>
<p>
There’s not really a right and wrong way to write code. That being said, <strong>this section outlines a strong argument for adopting an object-oriented approach in software development, especially in large applications.</strong>
</p>
<p></p>
<h2>Reason 1: Ease of Implementation</h2>
<blockquote class="pullquote pqRight">
<p>
“While it may be daunting at first, OOP actually provides an easier approach to dealing with data.”
</p>
</blockquote>
<p>
While it may be daunting at first, OOP actually provides an easier approach to dealing with data. Because an object can store data internally, variables don’t need to be passed from function to function to work properly.
</p>
<p>
Also, because <em>multiple instances of the same class can exist simultaneously</em>, dealing with large data sets is infinitely easier. For instance, imagine you have two people’s information being processed in a file. They need names, occupations, and ages.
</p>
<h3>The Procedural Approach</h3>
<p>
Here’s the procedural approach to our example:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code110'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p157code110"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'job'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Change the person's job</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">++</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Add 1 to the person's age</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tom'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Button-Pusher'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Lever-Puller'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the starting values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Tom got a promotion and had a birthday</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Box-Mover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// John just had a birthday</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the new values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
When executed, the code outputs the following:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code111'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157111"><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
</pre></td><td class="code" id="p157code111"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>
<span style="color: #009900;">&#41;</span>
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
While this code isn’t necessarily bad, there’s a lot to keep in mind while coding. <strong>The array of the affected person’s attributes must be passed and returned from each function call</strong>, which leaves margin for error.
</p>
<p>
To clean up this example, it would be desirable to <strong>leave as few things up to the developer as possible.</strong> Only absolutely essential information for the current operation should need to be passed to the functions.
</p>
<p>
<strong>This is where OOP steps in and helps you clean things up.</strong>
</p>
<h3>The OOP Approach</h3>
<p>
Here’s the OOP approach to our example:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code112'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code" id="p157code112"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_job</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_age</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$job</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$job</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age <span style="color: #339933;">=</span> <span style="color: #000088;">$age</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create two new people</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Button-Pusher&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">34</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lever Puller&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">41</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output their starting point</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Give Tom a promotion and a birthday</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">changeJob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Box-Mover&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// John just gets a year older</span>
<span style="color: #000088;">$person2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the ending values</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 1: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Person 2: &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</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;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
This outputs the following in the browser:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code113'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157113"><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" id="p157code113"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
There’s a little bit more setup involved to make the approach object oriented, but after the class is defined, creating and modifying people is a breeze; <strong>a person’s information does not need to be passed or returned from methods, and only absolutely essential information is passed to each method.</strong>
</p>
<blockquote class="pullquote pqRight">
<p>
“OOP will significantly reduce your workload if implemented properly.”
</p>
</blockquote>
<p>
On the small scale, this difference may not seem like much, but as your applications grow in size, OOP will significantly reduce your workload if implemented properly.
</p>
<p>
<strong>Tip</strong> — <em>Not everything needs to be object oriented.</em> A quick function that handles something small in one place inside the application does not necessarily need to be wrapped in a class. Use your best judgment when deciding between object-oriented and procedural approaches.
</p>
<p></p>
<h2>Reason 2: Better Organization</h2>
<p>
Another benefit of OOP is how well it lends itself to being <strong>easily packaged and cataloged.</strong> Each class can generally be kept in its own separate file, and if a uniform naming convention is used, accessing the classes is extremely simple.
</p>
<p>
Assume you’ve got an application with 150 classes that are called dynamically through a controller file at the root of your application filesystem. All 150 classes follow the naming convention <tt>class.classname.inc.php</tt> and reside in the <tt>inc</tt> folder of your application.
</p>
<p>
The controller can implement PHP’s <tt>__autoload()</tt> function to dynamically pull in only the classes it needs as they are called, rather than including all 150 in the controller file just in case or coming up with some clever way of including the files in your own code:
</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p157code114'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p157114"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p157code114"><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> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">'inc/class.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$class_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.inc.php'</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>

<p>
Having each class in a separate file also makes code more portable and easier to reuse in new applications without a bunch of copying and pasting.
</p>
<p></p>
<h2>Reason 3: Easier Maintenance</h2>
<p>
Due to the more compact nature of OOP when done correctly, <strong>changes in the code are usually much easier to spot</strong> and make than in a long spaghetti code procedural implementation.
</p>
<p>
If a particular array of information gains a new attribute, a procedural piece of software may require (in a worst-case scenario) that the new attribute be added to each function that uses the array.
</p>
<p>
An OOP application could potentially be updated as easily adding the new property and then adding the methods that deal with said property.
</p>
<p>
A lot of the benefits covered in this section are the product of <strong>OOP in combination with DRY programming practices.</strong> It is definitely possible to create easy-to-maintain procedural code that doesn’t cause nightmares, and it is equally possible to create awful object-oriented code.
</p>
<p></p>
<h2>Summary</h2>
<p>
At this point, you should feel comfortable with the object-oriented programming style. Learning OOP is a great way to take your programming to that next level. When implemented properly, OOP will help you produce easy-to-read, easy-to-maintain, portable code that will save you (and the developers who work with you) hours of extra work. Are you stuck on something that wasn’t covered in this article? Are you already using OOP and have some tips for beginners? Share them in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/01/08/tutorial-object-oriented-php-for-beginners/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Create a PHP/MySQL Powered Forum from Scratch</title>
		<link>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/</link>
		<comments>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 06:57:35 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[login system]]></category>
		<category><![CDATA[membership]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[register]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user system]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=141</guid>
		<description><![CDATA[In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in! Step 1: Creating Database Tables It’s always a good idea to start with creating a good data model when building an application. Let’s describe our [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in!</p>
<p><span id="more-10188"></span></p>
<h2><span>Step 1</span>: Creating Database Tables</h2>
<p>
It’s always a good idea to start with creating a good data model when building an application. Let’s describe our application in one sentence: We are going to make a forum which has <strong>users</strong> who create <strong>topics</strong> in various <strong>categories</strong>. Other users can <strong>post</strong> replies. As you can see, I highlighted a couple of nouns which represent our table names.
</p>
<h4>Users</h4>
<ul>
<li>Categories</li>
<li>Topics</li>
<li>Posts</li>
</ul>
<p>
	These three objects are related to each other, so we’ll process that in our table design. Take a look at the scheme below.
</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/606_forum/tut_files/1.png" border="0"></div>
<p>
	Looks pretty neat, huh? Every square is a database table. All the columns are listed in it and the lines between them represent the relationships. I’ll  explain them further, so it’s okay if it doesn’t make a lot of sense to you right now.</p>
<p><span id="more-141"></span></p>
<p>
	I’ll discuss each table by explaining the SQL, which I created using the scheme above. For your own scripts you can create a similar scheme and SQL too. Some editors like MySQL Workbench (the one I used) can generate .sql files too, but I would recommend learning SQL because it’s more fun to do it yourself. A SQL introduction can be found at <a href="http://w3schools.com/sql/sql_intro.asp">W3Schools</a>.
</p>
<h5>Users Table</h5>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code154'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141154"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code154"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users <span style="color: #66cc66;">&#40;</span>
user_id 	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
user_name	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_pass  	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_email	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_date	DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
user_level	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> user_name_unique <span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
	The CREATE TABLE statement is used to indicate we want to create a new table, of course. The statement is followed by the name of the table and all the columns are listed between the brackets. The names of all the fields are self-explanatory, so we’ll only discuss the data types below.
	</p>
<h5>user_id</h5>
<blockquote class="pullquote pqRight">
<p>
“A primary key is used to uniquely identify each row in a table.”
</p>
</blockquote>
<p>
		The type of this field is INT, which means this field holds an integer. The field cannot be empty (NOT NULL) and increments which each record inserted. At the bottom of the table you can see the user_id field is declared as a primary key. A primary key is used to uniquely identify each row in a table. No two distinct rows in a table can have the same value (or combination of values) in all columns. That might be a bit unclear, so here’s a little example.
		</p>
<p>
		There is a user called John Doe. If another users registers with the same name, there’s a problem, because: which user is which? You can’t tell and the database can’t tell either. By using a primary key this problem is solved, because both topics are unique.
		</p>
<p>
		All the other tables have got primary keys too and they work the same way.
	</p>
<h5>user_name</h5>
<p>		This is a text field, called a VARCHAR field in MySQL. The number between brackets is the maximum length. A user can choose a username up to 30 characters long. This field cannot be NULL. At the bottom of the table you can see this field is declared UNIQUE, which means the same username cannot be registered twice. The UNIQUE INDEX part tells the database we want to add a unique key. Then we define the name of the unique key, user_name_unique in this case. Between brackets is the field the unique key applies to, which is user_name.
	</p>
<h5>user_pass</h5>
<p>
		This field is equal to the user_name field, except the maximum length. Since the user password, no matter what length, is hashed with sha1(), the password will always be 40 characters long.
	</p>
<h5>user_email</h5>
<p>
		This field is equal to the user_pass field.
		</p>
<h5>user_date</h5>
<p>
		This is a field in which we’ll store the date the user registered. It’s type is DATETIME and the field cannot be NULL.</p>
<h5>user_level</h5>
<p>
		This field contains the level of the user, for example: ’0′ for a regular user and ’1′ for an admin. More about this later.</p>
</p>
<h4>Categories Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code155'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141155"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code155"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> categories <span style="color: #66cc66;">&#40;</span>
cat_id 		 	INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
cat_name	 	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
cat_description 	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> cat_name_unique <span style="color: #66cc66;">&#40;</span>cat_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cat_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
These data types basically work the same way as the ones in the users table. This table also has a primary key and the name of the category must be an unique one.
</p>
<h4>Topics Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code156'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141156"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code156"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #66cc66;">&#40;</span>
topic_id		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
topic_subject  		VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_date		DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_cat		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
topic_by		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>topic_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
		This table is almost the same as the other tables, except for the topic_by field. That field refers to the user who created the topic. The topic_cat refers to the category the topic belongs to. We cannot force these relationships by just declaring the field. We have to let the database know this field must contain an existing user_id from the users table, or a valid cat_id from the categories table. We’ll add some relationships after I’ve discussed the posts table.
	</p>
<h4>Posts Table</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code157'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141157"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code157"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #66cc66;">&#40;</span>
post_id 		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
post_content		TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_date 		DATETIME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_topic		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
post_by		INT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>post_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>INNODB;</pre></td></tr></table></div>

<p>
	This is the same as the rest of the tables; there’s also a field which refers to a user_id here: the post_by field. The post_topic field refers to the topic the post belongs to.
	</p>
<blockquote class="pullquote pqRight">
<p>
“A foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table.”
</p>
</blockquote>
<p>
	Now that we’ve executed these queries, we have a pretty decent data model, but the relations are still missing. Let’s start with the definition of a relationship. We’re going to use something called a foreign key. A foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. Some conditions:
	</p>
<ul>
<li>The column in the referencing table the foreign key refers to must be a primary key</li>
<li>The values that are referred to must exist in the referenced table</li>
</ul>
<p>
	By adding foreign keys the information is linked together which is very important for database normalization. Now you know what a foreign key is and why we’re using them. It’s time to add them to the tables we’ve already made by using the ALTER statement, which can be used to change an already existing table.
	</p>
<p>
	We’ll link the topics to the categories first:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code158'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141158"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code158"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>topic_cat<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> categories<span style="color: #66cc66;">&#40;</span>cat_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	The last part of the query already says what happens. When a category gets deleted from the database, all the topics will be deleted too. If the cat_id of a category changes, every topic will be updated too. That’s what the ON UPDATE CASCADE part is for. Of course, you can reverse this to protect your data, so that you can’t delete a category as long as it still has topics linked to it. If you would want to do that, you could replace the ‘ON DELETE CASCADE’ part with ‘ON DELETE RESTRICT’. There is also SET NULL and NO ACTION, which speak for themselves.
	</p>
<p>
	Every topic is linked to a category now. Let’s link the topics to the user who creates one.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code159'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141159"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code159"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> topics <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>topic_by<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> users<span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> RESTRICT <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	This foreign key is the same as the previous one, but there is one difference: the user can’t be deleted as long as there are still topics with the user id of the user. We don’t use CASCADE here because there might be valuable information in our topics. We wouldn’t want that information to get deleted if someone decides to delete their account. To still give users the opportunity to delete their account, you could build some feature that anonymizes all their topics and then delete their account. Unfortunately, that is beyond the scope of this tutorial.
	</p>
<p><strong><br />
	Link the posts to the topics:<br />
	</strong></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code160'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141160"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code160"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>post_topic<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> topics<span style="color: #66cc66;">&#40;</span>topic_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	<strong>And finally, link each post to the user who made it:</strong>
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code161'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141161"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code161"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>post_by<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> users<span style="color: #66cc66;">&#40;</span>user_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> RESTRICT <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> CASCADE;</pre></td></tr></table></div>

<p>
	That’s the database part! It was quite a lot of work, but the result, a great data model, is definitely worth it.
</p>
<p></p>
<h2><span>Step 2</span>: Introduction to the Header/Footer System</h2>
<p>
	Each page of our forum needs a few basic things, like a doctype and some markup. That’s why we’ll include a header.php file at the top of each page, and a footer.php at the bottom. The header.php contains a doctype, a link to the stylesheet and some important information about the forum, such as the title tag and metatags.
	</p>
<h5>header.php</h5>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code162'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141162"><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
</pre></td><td class="code" id="p141code162"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span>
<span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml<span style="color: #339933;">:</span>lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nl&quot;</span> lang<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nl&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>meta name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;A short description.&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>meta name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;keywords&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;put, keywords, here&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>PHP<span style="color: #339933;">-</span><a href="http://www.php.net/mysql"><span style="color: #990000;">MySQL</span></a> forum<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;style.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>My forum<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrapper&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;menu&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/index.php&quot;</span><span style="color: #339933;">&gt;</span>Home<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/create_topic.php&quot;</span><span style="color: #339933;">&gt;</span>Create a topic<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span>
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/forum/create_cat.php&quot;</span><span style="color: #339933;">&gt;</span>Create a category<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
&nbsp;
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>Hello Example<span style="color: #339933;">.</span> Not you? <a href="http://www.php.net/log"><span style="color: #990000;">Log</span></a> out<span style="color: #339933;">.&lt;/</span>div<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		The wrapper div will be used to make it easier to style the entire page. The menu div obviously contains a menu with links to pages we still have to create, but it helps to see where we’re going a little bit. The userbar div is going to be used for a small top bar which contains some information like the username and a link to the logout page. The content page holds the actual content of the page, obviously.
	</p>
<p>
		The attentive reader might have already noticed we’re missing some things. There is no </body> or </html> tag. They’re in the footer.php page, as you can see below.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code163'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141163"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p141code163"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--</span> content <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;&lt;!--</span> wrapper <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;footer&quot;</span><span style="color: #339933;">&gt;</span>Created <span style="color: #b1b100;">for</span> GigaSpartan<span style="color: #339933;">.</span>com<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		When we include a header and a footer on each page the rest of the page get embedded between the header and the footer. This method has got some advantages. First and foremost, everything will be styled correctly. A short example:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code164'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141164"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p141code164"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #666666; font-style: italic;">//the beautifully styled content, everything looks good</span>
 	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div id=&quot;content&quot;&gt;some text&lt;/div&gt;'</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: #666666; font-style: italic;">//bad looking, unstyled error :-(</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		As you can see, a page without errors will result in a nice page with the content. But if there’s an error, everything looks really ugly; so that’s why it’s better to make sure not only real content is styled correctly, but also the errors we might get.
	</p>
<p>
		Another advantage is the possibility of making quick changes. You can see for yourself by editing the text in footer.php when you’ve finished this tutorial; you’ll notice that the footer changes on every page immediately. Finally, we add a stylesheet which provides us with some basic markup – nothing too fancy.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code165'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141165"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
</pre></td><td class="code" id="p141code165"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#4E4E4E</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;">center</span><span style="color: #00AA00;">;</span>			<span style="color: #808080; font-style: italic;">/* make sure IE centers the page too */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#wrapper</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">900px</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: #993333;">auto</span><span style="color: #00AA00;">;</span> 			<span style="color: #808080; font-style: italic;">/* center the page */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</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: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</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: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>				<span style="color: #808080; font-style: italic;">/* fill up the entire div */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> <span style="color: #00AA00;">&#123;</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: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>		<span style="color: #808080; font-style: italic;">/* avoid a double border */</span>
	<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>				<span style="color: #808080; font-style: italic;">/* clear:both makes sure the content div doesn't float next to this one but stays under it */</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</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: #933;">30px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</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;">85%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> a<span style="color: #3333ff;">:hover </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: #cc00cc;">#009FC1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#userbar</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: #cc00cc;">#fff</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;">right</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* begin table styles */</span>
table <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
table a <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
table a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#373737</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>
&nbsp;
th <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#B40E1F</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F0F0F0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
td <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Begin font styles */</span>
h1<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F1F3F1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h3 <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: #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: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Menu styles */</span>
<span style="color: #6666ff;">.item</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: #cc00cc;">#00728B</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#032472</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</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>
&nbsp;
<span style="color: #6666ff;">.leftpart</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">70%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.rightpart</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.small</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#373737</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">65%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.topic-post</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
.post-<span style="color: #000000; font-weight: bold;">content</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
textarea <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2><span>Step 3</span>: Getting Ready for Action</h2>
<p>
		Before we can read anything from our database, we need a connection. That’s what connect.php is for. We’ll include it in every file we are going to create.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code166'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141166"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p141code166"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//connect.php</span>
<span style="color: #000088;">$server</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'usernamehere'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'passwordhere'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'databasenamehere'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #000088;">$username</span><span style="color: #339933;">,</span>  <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error: could not establish database connection'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error: could not select the database'</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>

<p>
		Simply replace the default values of the variables at the top of the page with your own date, save the file and you’re good to go!
	</p>
<p></p>
<h2><span>Step 4</span>: Displaying the Forum Overview</h2>
<p>
		Since we’re just started with some basic techniques, we’re going to make a simplified version of the forum overview for now.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code167'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141167"><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" id="p141code167"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;category.php?id=&quot;&gt;Category name&lt;/a&gt;&lt;/h3&gt; Category description goes here'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;topic.php?id=&quot;&gt;Topic subject&lt;/a&gt; at 10-10'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		There you have it: a nice and clean overview. We’ll be updating this page throughout the tutorial so that it becomes more like the end result, step by step!
	</p>
<p></p>
<h2><span>Step 5</span>: Signing up a User</h2>
<p>
		Let’s start by making a simple HTML form so that a new user can register.
	</p>
<p>
		A PHP page is needed to process the form. We’re going to use a $_SERVER variable. The $_SERVER variable is an array with values that are automatically set with each request. One of the values of the $_SERVER array is ‘REQUEST_METHOD’. When a page is requested with GET, this variable will hold the value ‘GET’. When a page is requested via POST, it will hold the value ‘POST’. We can use this value to check if a form has been posted. See the signup.php page below.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code168'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141168"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
</pre></td><td class="code" id="p141code168"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//signup.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;Sign up&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*the form hasn't been posted yet, display it
	  note that the action=&quot;&quot; will cause the form to post to the same page it is on */</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
 	 	Username: &lt;input type=&quot;text&quot; name=&quot;user_name&quot; /&gt;
 		Password: &lt;input type=&quot;password&quot; name=&quot;user_pass&quot;&gt;
		Password again: &lt;input type=&quot;password&quot; name=&quot;user_pass_check&quot;&gt;
		E-mail: &lt;input type=&quot;email&quot; name=&quot;user_email&quot;&gt;
 		&lt;input type=&quot;submit&quot; value=&quot;Add category&quot; /&gt;
 	 &lt;/form&gt;'</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: #666666; font-style: italic;">/* so, the form has been posted, we'll process the data in three steps:
		1.	Check the data
		2.	Let the user refill the wrong fields (if necessary)
		3.	Save the data
	*/</span>
	<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* declare the array for later use */</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//the user name exists</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/ctype_alnum"><span style="color: #990000;">ctype_alnum</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</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: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username can only contain letters and digits.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username cannot be longer than 30 characters.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username field must not be empty.'</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><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</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: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass_check'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The two passwords did not match.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The password field cannot be empty.'</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*check for an empty array, if there are errors, they're in this array (note the ! operator)*/</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Uh-oh.. a couple of fields are not filled in correctly..'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/* walk through the array so all the errors get displayed */</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* this generates a nice error list */</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</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: #666666; font-style: italic;">//the form has been posted without, so save it</span>
		<span style="color: #666666; font-style: italic;">//notice the use of mysql_real_escape_string, keep everything safe!</span>
		<span style="color: #666666; font-style: italic;">//also notice the sha1 function which hashes the password</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
					users(user_name, user_pass, user_email ,user_date, user_level)
				VALUES('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
					   '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
					   '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
						NOW(),
						0)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Something went wrong while registering. Please try again later.'</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//echo mysql_error(); //debugging purposes, uncomment when needed</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: #0000ff;">'Successfully registered. You can now &lt;a href=&quot;signin.php&quot;&gt;sign in&lt;/a&gt; and start posting! :-)'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		A lot of explanation is in the comments I made in the file, so be sure to check them out. The processing of the data takes place in three parts:
	</p>
<ul>
<li>Validating the data</li>
<li>If the data is not valid, show the form again</li>
<li>If the data is valid, save the record in the database</li>
</ul>
<p>
		The PHP part is quite self-explanatory. The SQL-query however probably needs a little more explanation.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code169'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141169"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code169"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
       users<span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">,</span> user_pass<span style="color: #66cc66;">,</span> user_email <span style="color: #66cc66;">,</span>user_date<span style="color: #66cc66;">,</span> user_level<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_name<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">'&quot; . sha1($_POST['</span>user_pass<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_email<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
       <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>
		On line 1 we have the INSERT INTO statement which speaks for itself. The table name is specified on the second line. The words between the brackets represent the columns in which we want to insert the data. The VALUES statement tells the database we’re done declaring column names and it’s time to specify the values. There is something new here: mysql_real_escape_string. The function escapes special characters in an unescaped string , so that it is safe to place it in a query. This function MUST always be used, with very few exceptions. There are too many scripts that don’t use it and can be hacked real easy. Don’t take the risk, use mysql_real_escape_string().
	</p>
<blockquote class="pullquote pqRight">
<p>
“Never insert a plain password as-is. You MUST always encrypt it.”
</p>
</blockquote>
<p>
		Also, you can see that the function sha1() is used to encrypt the user’s password. This is also a very important thing to remember. Never insert a plain password as-is. You MUST always encrypt it. Imagine a hacker who somehow manages to get access to your database. If he sees all the plain-text passwords he could log into any (admin) account he wants. If the password columns contain sha1 strings he has to crack them first which is almost impossible.
	</p>
<p>
		Note: it’s also possible to use md5(), I always use sha1() because benchmarks have proved it’s a tiny bit faster, not much though. You can replace sha1 with md5 if you like.
	</p>
<p>
		If the signup process was successful, you should see something like this:
	</p>
<p>
		Try refreshing your phpMyAdmin screen, a new record should be visible in the users table.
	</p>
<p></p>
<h2><span>Step 6</span>: Adding Authentication and User Levels</h2>
<p>
		An important aspect of a forum is the difference between regular users and admins/moderators. Since this is a small forum and adding features like adding new moderators and stuff would take way too much time, we’ll focus on the login process and create some admin features like creating new categories and closing a thread.
	</p>
<p>
		Now that you’ve completed the previous step, we’re going to make your freshly created account an admin account. In phpMyAdmin, click on the users table, and then ‘Browse’. Your account will probably pop up right away. Click the edit icon and change the value of the user_level field from 0 to 1. That’s it for now. You won’t notice any difference in our application immediately, but when we’ve added the admin features a normal account and your account will have different capabilities.
	</p>
<p>
		The sign-in process works the following way:
	</p>
<ul>
<li>A visitor enters user data and submits the form</li>
<li>If the username and password are correct, we can start a session</li>
<li>If the username and password are incorrect, we show the form again with a message</li>
</ul>
<p>
		The signin.php file is below. Don’t think I’m not explaining what I’m doing, but check out the comments in the file. It’s much easier to understand that way.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code170'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141170"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
</pre></td><td class="code" id="p141code170"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//signin.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;Sign in&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//first, check if the user is already signed in. If that is the case, there is no need to display this page</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</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;">echo</span> <span style="color: #0000ff;">'You are already signed in, you can &lt;a href=&quot;signout.php&quot;&gt;sign out&lt;/a&gt; if you want.'</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">/*the form hasn't been posted yet, display it
		  note that the action=&quot;&quot; will cause the form to post to the same page it is on */</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
			Username: &lt;input type=&quot;text&quot; name=&quot;user_name&quot; /&gt;
			Password: &lt;input type=&quot;password&quot; name=&quot;user_pass&quot;&gt;
			&lt;input type=&quot;submit&quot; value=&quot;Sign in&quot; /&gt;
		 &lt;/form&gt;'</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: #666666; font-style: italic;">/* so, the form has been posted, we'll process the data in three steps:
			1.	Check the data
			2.	Let the user refill the wrong fields (if necessary)
			3.	Varify if the data is correct and return the correct response
		*/</span>
		<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* declare the array for later use */</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</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: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The username field must not be empty.'</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: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</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: #000088;">$errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The password field must not be empty.'</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*check for an empty array, if there are errors, they're in this array (note the ! operator)*/</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Uh-oh.. a couple of fields are not filled in correctly..'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errors</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/* walk through the array so all the errors get displayed */</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* this generates a nice error list */</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</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: #666666; font-style: italic;">//the form has been posted without errors, so save it</span>
			<span style="color: #666666; font-style: italic;">//notice the use of mysql_real_escape_string, keep everything safe!</span>
			<span style="color: #666666; font-style: italic;">//also notice the sha1 function which hashes the password</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
						user_id,
						user_name,
						user_level
					FROM
						users
					WHERE
						user_name = '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'
					AND
						user_pass = '&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Something went wrong while signing in. Please try again later.'</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">//echo mysql_error(); //debugging purposes, uncomment when needed</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;">//the query was successfully executed, there are 2 possibilities</span>
				<span style="color: #666666; font-style: italic;">//1. the query returned data, the user can be signed in</span>
				<span style="color: #666666; font-style: italic;">//2. the query returned an empty result set, the credentials were wrong</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You have supplied a wrong user/password combination. Please try again.'</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: #666666; font-style: italic;">//set the $_SESSION['signed_in'] variable to TRUE</span>
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages</span>
					<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
&nbsp;
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Welcome, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. &lt;a href=&quot;index.php&quot;&gt;Proceed to the forum overview&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		This is the query that’s in the signin.php file:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code171'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141171"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code171"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
	user_id<span style="color: #66cc66;">,</span>
	user_name<span style="color: #66cc66;">,</span>
	user_level
<span style="color: #993333; font-weight: bold;">FROM</span>
	users
<span style="color: #993333; font-weight: bold;">WHERE</span>
	user_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>user_name<span style="color: #ff0000;">']) . &quot;'</span>
<span style="color: #993333; font-weight: bold;">AND</span>
	user_pass <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot; . sha1($_POST['</span>user_pass<span style="color: #ff0000;">'])</span></pre></td></tr></table></div>

<p>
		It’s obvious we need a check to tell if the supplied credentials belong to an existing user. A lot of scripts retrieve the password from the database and compare it using PHP. If we do this directly via SQL the password will be stored in the database once during registration and never leave it again. This is safer, because all the real action happens in the database layer and not in our application.
	</p>
<p>
		If the user is signed in successfully, we’re doing a few things:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code172'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141172"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code172"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//set the $_SESSION['signed_in'] variable to TRUE</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</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>

<p>
		First, we set the ‘signed_in’ $_SESSION var to true, so we can use it on other pages to make sure the user is signed in. We also put the username and user id in the $_SESSION variable for usage on a different page. Finally, we display a link to the forum overview so the user can get started right away.
	</p>
<p>
		Of course signing in requires another function, signing out! The sign-out process is actually a lot easier than the sign-in process. Because all the information about the user is stored in $_SESSION variables, all we have to do is unset them and display a message.
	</p>
<p>
		Now that we’ve set the $_SESSION variables, we can determine if someone is signed in. Let’s make a last simple change to header.php:
	</p>
<p>
		Replace:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code173'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141173"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code173"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;userbar&quot;&gt;Hello Example. Not you? Log out.&lt;/div&gt;</pre></td></tr></table></div>

<p>
		With:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code174'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141174"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p141code174"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userbar&quot;</span><span style="color: #339933;">&gt;</span>
 	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
 	<span style="color: #009900;">&#123;</span>
 	 	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. Not you? &lt;a href=&quot;signout.php&quot;&gt;Sign out&lt;/a&gt;'</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: #0000ff;">'&lt;a href=&quot;signin.php&quot;&gt;Sign in&lt;/a&gt; or &lt;a href=&quot;sign up&quot;&gt;create an account&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
 	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>
		If a user is signed in, he will see his or her name displayed on the front page with a link to the signout page. Our authentication is done! By now our forum should look like this:
	</p>
<p></p>
<h2><span>Step 7</span>: Creating a Category</h2>
<p>
		We want to create categories so let’s start with making a form.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code175'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141175"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p141code175"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
 	Category name: &lt;input type=&quot;text&quot; name=&quot;cat_name&quot; /&gt;
 	Category description: &lt;textarea name=&quot;cat_description&quot; /&gt;&lt;/textarea&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Add category&quot; /&gt;
 &lt;/form&gt;</pre></td></tr></table></div>

<p>
		This step looks a lot like Step 4 (Signing up a user’), so I’m not going to do an in-depth explanation here. If you followed all the steps you should be able to understand this somewhat quickly.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code176'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141176"><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
</pre></td><td class="code" id="p141code176"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//the form hasn't been posted yet, display it</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method='</span>post<span style="color: #0000ff;">' action='</span><span style="color: #0000ff;">'&gt;
 	 	Category name: &lt;input type='</span>text<span style="color: #0000ff;">' name='</span>cat_name<span style="color: #0000ff;">' /&gt;
 		Category description: &lt;textarea name='</span>cat_description<span style="color: #0000ff;">' /&gt;&lt;/textarea&gt;
 		&lt;input type='</span>submit<span style="color: #0000ff;">' value='</span>Add category<span style="color: #0000ff;">' /&gt;
 	 &lt;/form&gt;'</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: #666666; font-style: italic;">//the form has been posted, so save it</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> ìINSERT INTO categories<span style="color: #009900;">&#40;</span>cat_name<span style="color: #339933;">,</span> cat_description<span style="color: #009900;">&#41;</span>
 	   VALUES<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> ì<span style="color: #0000ff;">',
 		     '</span><span style="color: #0000ff;">' . mysql_real_escape_string($_POST['</span>cat_description<span style="color: #0000ff;">']) . ì'</span><span style="color: #009900;">&#41;</span><span style="color: #0000ff;">';
    $result = mysql_query($sql);
    if(!$result)
    {
        //something went wrong, display the error
        echo '</span>Error<span style="color: #0000ff;">' . mysql_error();
    }
    else
    {
        echo '</span><span style="color: #000000; font-weight: bold;">New</span> category successfully added<span style="color: #339933;">.</span><span style="color: #0000ff;">';
    }
}
?&gt;</span></pre></td></tr></table></div>

<p>
		As you can see, we’ve started the script with the $_SERVER check, after checking if the user has admin rights, which is required for creating a category. The form gets displayed if it hasn’t been submitted already. If it has, the values are saved. Once again, a SQL query is prepared and then executed.
	</p>
<p></p>
<h2><span>Step 8</span>: Adding Categories to index.php</h2>
<p>
		We’ve created some categories, so now we’re able to display them on the front page. Let’s add the following query to the content area of index.php.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code177'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141177"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p141code177"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
 	categories<span style="color: #66cc66;">.</span>cat_id<span style="color: #66cc66;">,</span>
	categories<span style="color: #66cc66;">.</span>cat_name<span style="color: #66cc66;">,</span>
 	categories<span style="color: #66cc66;">.</span>cat_description<span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
 	categories</pre></td></tr></table></div>

<p>
		This query selects all categories and their names and descriptions from the categories table. We only need a bit of PHP to display the results. If we add that part just like we did in the previous steps, the code will look like this.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code178'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141178"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code" id="p141code178"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
			cat_id,
			cat_name,
			cat_description,
		FROM
			categories&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The categories could not be displayed, please try again later.'</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;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No categories defined yet.'</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: #666666; font-style: italic;">//prepare the table</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table border=&quot;1&quot;&gt;
			  &lt;tr&gt;
				&lt;th&gt;Category&lt;/th&gt;
				&lt;th&gt;Last topic&lt;/th&gt;
			  &lt;/tr&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;category.php?id&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;/h3&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;a href=&quot;topic.php?id=&quot;&gt;Topic subject&lt;/a&gt; at 10-10'</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		Notice how we’re using the cat_id to create links to category.php. All the links to this page will look like this: category.php?cat_id=x, where x can be any numeric value. This may be new to you. We can check the url with PHP for $_GET values. For example, we have this link:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code179'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141179"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p141code179"><pre class="html" style="font-family:monospace;">category.php?cat_id=23</pre></td></tr></table></div>

<p>
		The statement echo $_GET[ëcat_id'];’ will display ’23′. In the next few steps we’ll use this value to retrieve the topics when viewing a single category, but topics can’t be viewed if we haven’t created them yet. So let’s create some topics!
	</p>
<p></p>
<h2><span>Step 9</span>: Creating a Topic</h2>
<p>
		In this step, we’re combining the techniques we learned in the previous steps. We’re checking if a user is signed in, we’ll use an input query to create the topic and create some basic HTML forms.
	</p>
<p>
		The structure of create_topic.php can hardly be explained in a list or something, so I rewrote it in pseudo-code.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code180'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141180"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p141code180"><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>user is signed in<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//the user is not signed in</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;">//the user is signed in</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>form has not been posted<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//show 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;">//process form</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		Here’s the real code of this part of our forum, check the explanations below the code to see what it’s doing.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code181'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141181"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
</pre></td><td class="code" id="p141code181"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h2&gt;Create a topic&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//the user is not signed in</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Sorry, you have to be &lt;a href=&quot;/forum/signin.php&quot;&gt;signed in&lt;/a&gt; to create a topic.'</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: #666666; font-style: italic;">//the user is signed in</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//the form hasn't been posted yet, display it</span>
		<span style="color: #666666; font-style: italic;">//retrieve the categories from the database for use in the dropdown</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
					cat_id,
					cat_name,
					cat_description
				FROM
					categories&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//the query failed, uh-oh :-(</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Error while selecting from database. Please try again later.'</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;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//there are no categories, so a topic can't be posted</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_level'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You have not created categories yet.'</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: #0000ff;">'Before you can post a topic, you must wait for an admin to create some categories.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
					Subject: &lt;input type=&quot;text&quot; name=&quot;topic_subject&quot; /&gt;
					Category:'</span><span style="color: #339933;">;</span> 
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;select name=&quot;topic_cat&quot;&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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: #0000ff;">'&lt;option value=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/select&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message: &lt;textarea name=&quot;post_content&quot; /&gt;&lt;/textarea&gt;
					&lt;input type=&quot;submit&quot; value=&quot;Create topic&quot; /&gt;
				 &lt;/form&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</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;">//start the transaction</span>
		<span style="color: #000088;">$query</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BEGIN WORK;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Damn! the query failed, quit</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while creating your topic. Please try again later.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//the form has been posted, so save it</span>
			<span style="color: #666666; font-style: italic;">//insert the topic into the topics table first, then we'll save the post into the posts table</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
						topics(topic_subject,
							   topic_date,
							   topic_cat,
							   topic_by)
				   VALUES('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_subject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
							   NOW(),
							   &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_cat'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
							   &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;
							   )&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while inserting your data. Please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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>
				<span style="color: #666666; font-style: italic;">//the first query worked, now start the second, posts query</span>
				<span style="color: #666666; font-style: italic;">//retrieve the id of the freshly created topic for usage in the posts query</span>
				<span style="color: #000088;">$topicid</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_insert_id"><span style="color: #990000;">mysql_insert_id</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
							posts(post_content,
								  post_date,
								  post_topic,
								  post_by)
						VALUES
							('&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'post_content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
								  NOW(),
								  &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$topicid</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
								  &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;
							)&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">//something went wrong, display the error</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'An error occured while inserting your post. Please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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>
					<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;COMMIT;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">//after a lot of work, the query succeeded!</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You have successfully created &lt;a href=&quot;topic.php?id='</span><span style="color: #339933;">.</span> <span style="color: #000088;">$topicid</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;your new topic&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		I’ll discuss this page in two parts, showing the form and processing the form.
	</p>
<p>
	<strong>Showing the form</strong><br />
	We’re starting with a simple HTML form. There is actually something special here, because we use a dropdown. This dropdown is filled with data from the database, using this query:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code182'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141182"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p141code182"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
 	cat_id<span style="color: #66cc66;">,</span>
 	cat_name<span style="color: #66cc66;">,</span>
 	cat_description
<span style="color: #993333; font-weight: bold;">FROM</span>
 	categories</pre></td></tr></table></div>

<p>
		That’s the only potentially confusing part here; it’s quite a piece of code, as you can see when looking at the create_topic.php file at the bottom of this step.
	</p>
<p>	<strong>Processing the form</strong></p>
<p>
		The process of saving the topic consists of two parts: saving the topic in the topics table and saving the first post in the posts table. This requires something quite advanced that goes a bit beyond the scope of this tutorial. It’s called a transaction, which basically means that we start by executing the start command and then rollback when there are database errors and commit when everything went well.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code183'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141183"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p141code183"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//start the transaction</span>
<span style="color: #000088;">$query</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BEGIN WORK;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//stop the transaction</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ROLLBACK;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//commit the transaction</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;COMMIT;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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>
		The first query being used to save the data is the topic creation query, which looks like this:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code184'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141184"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p141code184"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
	topics<span style="color: #66cc66;">&#40;</span>topic_subject<span style="color: #66cc66;">,</span>
               topic_date<span style="color: #66cc66;">,</span>
               topic_cat<span style="color: #66cc66;">,</span>
               topic_by<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>topic_subject<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
       NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_POST['topic_cat']) . &quot;</span><span style="color: #66cc66;">,</span>
       <span style="color: #ff0000;">&quot; . $_SESSION['user_id'] . &quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>
		At first the fields are defined, then the values to be inserted. We’ve seen the first one before, it’s just a string which is made safe by using mysql_real_escape_string(). The second value, NOW(), is a SQL function for the current time. The third value, however, is a value we haven’t seen before. It refers to a (valid) id of a category. The last value refers to an (existing) user_id which is, in this case, the value of $_SESSION[ëuser_id']. This variable was declared during the sign in process.
	</p>
<p>
		If the query executed without errors we proceed to the second query. Remember we are still doing a transaction here. If we would’ve got errors we would have used the ROLLBACK command.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code185'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141185"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p141code185"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span>
        posts<span style="color: #66cc66;">&#40;</span>post_content<span style="color: #66cc66;">,</span>
        post_date<span style="color: #66cc66;">,</span>
        post_topic<span style="color: #66cc66;">,</span>
        post_by<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot; . mysql_real_escape_string($_POST['</span>post_content<span style="color: #ff0000;">']) . &quot;'</span><span style="color: #66cc66;">,</span>
         NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
         <span style="color: #ff0000;">&quot; . $topicid . &quot;</span><span style="color: #66cc66;">,</span>
         <span style="color: #ff0000;">&quot; . $_SESSION['user_id'] . &quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>
		The first thing we do in this code is use mysql_insert_id() to retrieve the latest generated id from the topic_id field in the topics table. As you may remember from the first steps of this tutorial, the id is generated in the database using auto_increment.
	</p>
<p>
		Then the post is inserted into the posts table. This query looks a lot like the topics query. The only difference is that this post refers to the topic and the topic referred to a category. From the start, we decided to create a good data model and here is the result: a nice hierarchical structure.
	</p>
<p></p>
<h2><span>Step 10</span>: Category View</h2>
<p>
		We’re going to make an overview page for a single category. We’ve just created a category, it would be handy to be able to view all the topics in it. First, create a page called category.php.
	</p>
<p>
		A short list of the things we need:
	</p>
<h4>Needed for displaying the category</h4>
<ul>
<li>cat_name</li>
<li>cat_description</li>
</ul>
<p>	<i>Needed for displaying all the topics</i></p>
<ul>
<li>topic_id</li>
<li>topic_subject</li>
<li>topic_date</li>
<li>topic_cat</li>
</ul>
<p>
		Let’s create the two SQL queries that retrieve exactly this data from the database.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code186'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141186"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p141code186"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    cat_id<span style="color: #66cc66;">,</span>
    cat_name<span style="color: #66cc66;">,</span>
    cat_description
<span style="color: #993333; font-weight: bold;">FROM</span>
    categories
<span style="color: #993333; font-weight: bold;">WHERE</span>
    cat_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		The query above selects all the categories from the database.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code187'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141187"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p141code187"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    topic_id<span style="color: #66cc66;">,</span>
    topic_subject<span style="color: #66cc66;">,</span>
    topic_date<span style="color: #66cc66;">,</span>
    topic_cat
<span style="color: #993333; font-weight: bold;">FROM</span>
    topics
<span style="color: #993333; font-weight: bold;">WHERE</span>
    topic_cat <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		The query above is executed in the while loop in which we echo the categories. By doing it this way, we’ll see all the categories and the latest topic for each of them.<br />
		The complete code of category.php will be the following:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code188'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141188"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code" id="p141code188"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//first select the category based on $_GET['cat_id']</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
			cat_id,
			cat_name,
			cat_description
		FROM
			categories
		WHERE
			cat_id = &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The category could not be displayed, please try again later.'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</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>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This category does not exist.'</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: #666666; font-style: italic;">//display category data</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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: #0000ff;">'&lt;h2&gt;Topics in ′'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cat_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'′ category&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//do a query for the topics</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT
					topic_id,
					topic_subject,
					topic_date,
					topic_cat
				FROM
					topics
				WHERE
					topic_cat = &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The topics could not be displayed, please try again later.'</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;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'There are no topics in this category yet.'</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: #666666; font-style: italic;">//prepare the table</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;table border=&quot;1&quot;&gt;
					  &lt;tr&gt;
						&lt;th&gt;Topic&lt;/th&gt;
						&lt;th&gt;Created at&lt;/th&gt;
					  &lt;/tr&gt;'</span><span style="color: #339933;">;</span>	
&nbsp;
				<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</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: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;leftpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h3&gt;&lt;a href=&quot;topic.php?id='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_subject'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;h3&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;td class=&quot;rightpart&quot;&gt;'</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d-m-Y'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'topic_date'</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: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		And here is the final result of our categories page:
	</p>
<p></p>
<h2><span>Step 11</span>: Topic View</h2>
<p>
		The SQL queries in this step are complicated ones. The PHP-part is all stuff that you’ve seen before. Let’s take a look at the queries. The first one retrieves basic information about the topic:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code189'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141189"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p141code189"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    topic_id<span style="color: #66cc66;">,</span>
    topic_subject
<span style="color: #993333; font-weight: bold;">FROM</span>
    topics
<span style="color: #993333; font-weight: bold;">WHERE</span>
    topics<span style="color: #66cc66;">.</span>topic_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		This information is displayed in the head of the table we will use to display all the data. Next, we retrieve all the posts in this topic from the database. The following query gives us exactly what we need:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code190'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141190"><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" id="p141code190"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    posts<span style="color: #66cc66;">.</span>post_topic<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_content<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_date<span style="color: #66cc66;">,</span>
    posts<span style="color: #66cc66;">.</span>post_by<span style="color: #66cc66;">,</span>
    users<span style="color: #66cc66;">.</span>user_id<span style="color: #66cc66;">,</span>
    users<span style="color: #66cc66;">.</span>user_name
<span style="color: #993333; font-weight: bold;">FROM</span>
    posts
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
    users
<span style="color: #993333; font-weight: bold;">ON</span>
    posts<span style="color: #66cc66;">.</span>post_by <span style="color: #66cc66;">=</span> users<span style="color: #66cc66;">.</span>user_id
<span style="color: #993333; font-weight: bold;">WHERE</span>
    posts<span style="color: #66cc66;">.</span>post_topic <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; . mysql_real_escape_string($_GET['id'])</span></pre></td></tr></table></div>

<p>
		This time, we want information from the users and the posts table – so we use the LEFT JOIN again. The condition is: the user id should be the same as the post_by field. This way we can show the username of the user who replied at each post.</p>
<p>
		The final topic view looks like this:
	</p>
<p></p>
<h2><span>Step 12</span>: Adding a Reply</h2>
<p>
		Let’s create the last missing part of this forum, the possibility to add a reply. We’ll start by creating a form:
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code191'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141191"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p141code191"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;reply.php?id=5&quot;&gt;
    &lt;textarea name=&quot;reply-content&quot;&gt;&lt;/textarea&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit reply&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>
		The complete reply.php code looks like this.
	</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code192'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p141192"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p141code192"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//create_cat.php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'connect.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'header.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//someone is calling the file directly, which we don't want</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This file cannot be called directly.'</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: #666666; font-style: italic;">//check for sign in status</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'signed_in'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'You must be signed in to post a reply.'</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: #666666; font-style: italic;">//a real user posted a real reply</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO
					posts(post_content,
						  post_date,
						  post_topic,
						  post_by)
				VALUES ('&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'reply-content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;',
						NOW(),
						&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,
						&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Your reply has not been saved, please try again later.'</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: #0000ff;">'Your reply has been saved, check out &lt;a href=&quot;topic.php?id='</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/htmlentities"><span style="color: #990000;">htmlentities</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;the topic&lt;/a&gt;.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'footer.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
		The comments in the code pretty much detail what’s happening. We’re checking for a real user and then inserting the post into the database.
	</p>
<p></p>
<h2>Finishing Up</h2>
<p>
		Now that you’ve finished this tutorial, you should have a much better understanding of what it takes to build a forum. I hope my explanations were clear enough! Thanks again for reading.
	</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2011/01/03/tutorial-create-a-phpmysql-powered-forum-from-scratch/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Tutorial: Simple Techniques to Lock Down your Website</title>
		<link>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/</link>
		<comments>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 15:11:22 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[cryptology]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[randomization]]></category>
		<category><![CDATA[secuirty]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=123</guid>
		<description><![CDATA[One crucial part of PHP development practice is always keeping in mind that security is not something you can simply buy off the shelf at your local convenient store. Ensuring the security of your web applications is a process, which over time, needs to be constantly evaluated, monitored, and hardened. Introduction While the use of [...]]]></description>
			<content:encoded><![CDATA[<p>One crucial part of PHP development practice is always keeping in mind that security is not something you can simply buy off the shelf at your local convenient store.  Ensuring the security of your web applications is a process, which over time, needs to be constantly evaluated, monitored, and hardened.</p>
<p><span id="more-7035"></span></p>
<p></p>
<h2>Introduction</h2>
<p>While the use of filters and validating data is one part of the security process, a web developer should be aware that Randomization, Obfuscation, and Cryptography in PHP can make a difference in the security of web applications.  This tutorial will guide you through some simple techniques at creating and using random or unique values within your web applications, taking a look and applying some general obfuscation techniques, and looking deeper into the science of Cryptology and it’s use within PHP.</p>
<p></p>
<h2>What you Will Learn</h2>
<ul>
<li>How to generate random values with PHP</li>
<li>Generating random Passwords</li>
<li>Salting Passwords and Authenticating The User</li>
<li>Obfuscation in PHP, an Overview</li>
<li>Cryptography in PHP and it’s Applications</li>
</ul>
<p><span id="more-123"></span><br />
</p>
<h2>Generating Random Values</h2>
<p>Dictionary.com defines randomization as:</p>
<blockquote><p>“-verb: to order or select in a random manner, as in a sample or experiment, especially in order to reduce bias and interference caused by irrelevant variables; make random.”</p>
</blockquote>
<p>Random number generation is determined in a variety of ways, however computational generators fall short of ‘true’ randomness as seen in nature or electronic noise(the fuzzy, screeching, black and white channel on TV).  These computed values are regarded as pseudo-random.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/whitenoise.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/whitenoise.jpg" style="display: inline; "></div>
<p>PHP provides us with a couple of different ways to create random values. Let’s look at a few of the more popular functions.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code208'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123208"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p123code208"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span>int <span style="color: #000088;">$min</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span>int <span style="color: #000088;">$min</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$prefix</span><span style="color: #339933;">,</span> more_entropy<span style="color: #339933;">=</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>The two functions <code>rand()</code> and <code>mt_rand() </code>are likely the most widely used functions to generate a set of random numbers in PHP.  The function<code> rand()</code>; is an older generator, and is falling out of use due to <code>mt_rand()</code>; which is faster, more reliable, and can handle a higher maximum integer value on some platforms. The function <code>str_shuffle() </code>does exactly what you would expect it to, it shuffles a string passed to it.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code209'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123209"><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
</pre></td><td class="code" id="p123code209"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Examples of mt_rand() usage</span>
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Outputs a random integer between 0 and 20</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Examples of rand() usage</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Outputs a random integer between 0 and 25</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Example of str_shuffle usage</span>
&nbsp;
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcefghijklmnopqrstuvwxyz'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//shuffles $string</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <code>rand()</code> and <code>mt_rand()</code> functions both accept two parameters where <code>$min</code> is the lowest integer to start with, and <code>$max</code> being the largest integer to end with. The function <code>str_shuffle</code> takes one parameter, a string, outputting a shuffled mutation of the string. It acts the same as if you were shuffling a deck of cards.</p>
<p>While <code>mt_rand(); </code>will spit out a random integer, and <code>str_shuffle </code>will mix a string up, a function widely used to create random unique values is <code>uniqid()</code>.  This generates a prefixed unique identifier based on the current time in microseconds(<a href="http://us.php.net/manual/en/function.uniqid.php" title="PHP uniqid manual">via php.net</a>).</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code210'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123210"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p123code210"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Examples of uniqid() usage</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//default</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GIGASPARTAN&quot;</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: #666666; font-style: italic;">//Adding an additional prefix and setting more_entropy to TRUE</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The function <code>uniqid()</code> accepts two parameters the first appends a prefix to the results while the second, if set to TRUE, will add additional <a href="http://en.wikipedia.org/wiki/Information_entropy" title="Wikipedia: Entropy">entropy</a> to the end of the returned value.</p>
<p></p>
<h2>Generating Random Passwords</h2>
<p>There are a gazillion examples on the web which generate random passwords, all do a fine job at it.  “But why,” you ask “would I need to generate a random password?”  Well the answer, quite simply, is so you do not have to rely on the end user to provide themselves with a less than secure password at the get go.  Generating random passwords is very useful in user registrations, or when a user makes a request because they have forgotten their password.  Doing this ensures a strong password at the beginning of a users experience at your website, or can cut down lines of code when a user needs to gain access again.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/shuffle.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/shuffle.jpg" style="display: inline; "></div>
<p>Let’s look at some examples:Example 1</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code211'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123211"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p123code211"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//A simple function which will output a random password</span>
<span style="color: #000000; font-weight: bold;">function</span> randompassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//returns the password</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This example shuffles a string with <code>str_shuffle</code> and will return a string within a counted range.  So if you wanted to generate an 8 character password then you would pass 8 to the function randompassword, or randompassword(8) from your source code.</p>
<p>Example 2</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code212'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123212"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p123code212"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Another example to create a random password</span>
<span style="color: #000000; font-weight: bold;">function</span> anorandpass<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$m_rand</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//generate a random integer</span>
&nbsp;
	<span style="color: #000088;">$u_id</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MNO!@#$%^&amp;amp;*=+XYZ&quot;</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: #666666; font-style: italic;">//create a unique identifier with some extra prefix and extra entropy</span>
&nbsp;
	<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$m_rand</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$u_id</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// Combine the variables to form a string</span>
&nbsp;
	<span style="color: #000088;">$new</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_shuffle"><span style="color: #990000;">str_shuffle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//shuffle our string</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//return the password</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> anorandpass<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In comparison, example one takes a static string and mixes it up then returns it, example two adds in more dynamic flavor(mmm tasty).  In example two the string being shuffled is no longer static, but changes with each generation.  While the first example is certainly sufficient in most cases to generate a strong password, the second example allows us to ensure the string length and characters will change with use, greatly decreasing the chance of a duplication.</p>
<p>Enforcing the use of strong passwords within a web application will deter users from visiting or signing up for a website.  It is often a trade off between getting the traffic you desire, and ensuring the security of the application.  I suggest allowing your users to create their own passwords at sign-up, or allow them to choose between the two.</p>
<p></p>
<h2>Please Pass the Salt.  Salting Passwords for Increased Security.</h2>
<p>Salting passwords is an effective way to increase the security of your users accounts even if an attacker gains access to your database, if done right.  It can be argued that, with access to the salt, an attacker can still gain your credentials.  While this is true, applying some randomization techniques to the storage of passwords will make that process extremely difficult, especially if the storage of user information and content are divided into separate databases.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/elements.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/elements.jpg" style="display: inline; "></div>
<p></p>
<h2>Why and How?</h2>
<p>Again this falls under the “non-reliance of the end-user to provide themselves simple security” measure.  Users generally use passwords which are easy to remember, and even use the same passwords across multiple websites(I know, right!?). Easy to remember passwords are generally words found in a dictionary and other kinds of values(ex. 12345, QWERTY).  As developers we often scoff at this practice, but we cannot deny that it’s just the way things are.</p>
<p>In order for a web application to utilize a salt in a password, the application has to store it somewhere.  It’s not recommended to use the same salt across an entire database of passwords, but to generate a unique salt per user.  Generating one salt for an entire database actually decreases the security of the web application in a sense that if an attacker manages to crack it the entire scheme is broke, or if lost, renders the database useless. Creating a full fledged member registration system with all the bells and whistles is out of the scope of this tutorial, however we will be creating a simple system to use an example. Let’s look at generating a salt and applying some randomization techniques:</p>
<p></p>
<h2>1. The Database Connection</h2>
<p>Here is the SQL table that we will be using.</p>
<pre name="code">CREATE TABLE IF NOT EXISTS `users` (
  `usr_id` int(11) NOT NULL AUTO_INCREMENT,
  `usr_name` varchar(24) NOT NULL,
  `usr_pass` varchar(32) NOT NULL,
  `usr_email` varchar(255) NOT NULL,
  `usr_salt` varchar(255) NOT NULL,
  PRIMARY KEY (`usr_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;
</pre>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code213'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123213"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p123code213"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*db_config.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//database configuration</span>
<span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;localhost&quot;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//will likely stay the same</span>
<span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;thedbname&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//the name of the database table</span>
<span style="color: #000088;">$db_usr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//your database username</span>
<span style="color: #000088;">$db_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//your database password</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Establish a connection with MySQL and select the database to use</span>
<a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_usr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_pass</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_name</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>2. The Registration File</h2>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code214'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123214"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p123code214"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*registration.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//require our db_config.php file</span>
<span style="color: #b1b100;">require</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db_config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Check to see if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>  <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Escape our posted inputs</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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;">//generate a strong unique salt</span>
	<span style="color: #000088;">$salt_gen</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Combine email, the password and the salt together</span>
	<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$salt_gen</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//md5 hash the combined password * Note: md5 is only used in this scenario as an example</span>
	<span style="color: #000088;">$newpassword</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//insert the values into the database</span>
	<span style="color: #000088;">$registerquery</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO users (usr_name, usr_pass, usr_email,  usr_salt) VALUES ('&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$newpassword</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$email</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$salt_gen</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//let the user know of success or failure</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$registerquery</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Success&lt;/h1&gt;'</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: #0000ff;">'&lt;h1&gt;Failure&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Let’s go over the PHP code.  To keep things simple we include our database config file.  Next PHP checks to see if the form HTML has been submitted by checking if the <code>$_POST</code> variables are not empty.  If they are not empty then the script proceeds to escape the posted form data from the user, preparing it to be inserted into the database.  We then generate a simple salt using <code>uniqid()</code> and <code>mt_rand()</code> and storing it in the variable <code>$salt_gen</code>.  To salt our password we combine the $password, then the salt. Next step, one way hashing the combined variables with md5.</p>
<p>“But wait! You also added the users email to the front of the password and salt combo!” Yup! I did this because, if an attacker gains access to my database in some way, and the salt, the only way the attacker is going to know for sure that the email address is used in the hashing of the password is if they have access to the source code.  How random and unique is an email address?</p>
<p>To top the rest of the PHP code off we insert our variables into the database table within their respective fields, and give the user some feedback on success or failure. Now onto the rest of the registration file, the HTML</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code215'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123215"><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
</pre></td><td class="code" id="p123code215"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;label for=&quot;username&quot;&gt;Enter a Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;email&quot;&gt;Enter your Email&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;password&quot;&gt;Enter a Password&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&nbsp;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Here we create a simple HTML form which will collect a username, an email, and a password from a user. Nothing fancy here.</p>
<p></p>
<h2>3. Authenticating the User</h2>
<p>So we now have a simple registration form, which inserts a user into the database along with their salted password.  Let’s create a login page which will require us to retrieve information from the database and authenticate the user. First the PHP:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code216'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123216"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p123code216"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*login.php*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//require our db_config.php file</span>
<span style="color: #b1b100;">require</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db_config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Check to see if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>  <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Escape our posted inputs</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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;">//Grab the row associated with the username from the form</span>
	<span style="color: #000088;">$grab_row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usr_name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//If only one row was retrieved</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$grab_row</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//create an array from the row fields</span>
		<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array"><span style="color: #990000;">mysql_fetch_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$grab_row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//store the users salt in a var</span>
		<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'usr_salt'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//store the users email in a var</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'usr_email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//recombine the variables email, password, and the salt</span>
		<span style="color: #000088;">$combine</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$salt</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//re-hash the combined variables Note:md5 is only used in this scenario as an example</span>
		<span style="color: #000088;">$auth_pass</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//check the database again for the row associated with the username and the rehashed password</span>
		<span style="color: #000088;">$checklogin</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM users WHERE usr_name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND usr_pass = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$auth_pass</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQL Error: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//if only one row is retrieved output success or failure to the user</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$checklogin</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Yippie, we are authenticated!&lt;/h1&gt;'</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: #0000ff;">'&lt;h1&gt;Oh no, we are not authenticated!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</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: #0000ff;">'&lt;h1&gt;Oh no, we are not in the database!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Basically what we are doing in the login.php file is taking the submitted form variables, grabbing the table row associated with the username and rebuilding the password from the elements in the database it was created with (email, pass, salt) and rehashing them.  We then check the database again for the username AND the rehashed password value to find a match, outputting the user on success or failure. Finally here is the HTML:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code217'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123217"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p123code217"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;label for=&quot;username&quot;&gt;Enter your Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;label for=&quot;password&quot;&gt;Enter a Password&lt;label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; /&gt;&lt;br /&gt;
&nbsp;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p></p>
<h2>Obfuscation in PHP</h2>
<p>A simple yet complex definition of obfuscation is (use the version contained in the source if you wish to run the code):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code218'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123218"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p123code218"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$a1c0_z2</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">.</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'tion '</span><span style="color: #339933;">;</span><span style="color: #000088;">$a91</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$vly_ti</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;us&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'ed'</span><span style="color: #339933;">;</span><span style="color: #000088;">$j1h_32_a</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' to'</span><span style="color: #339933;">;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">=</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$lz32i_4</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;“O&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;bfus&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$g1k0p</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'que '</span><span style="color: #339933;">;</span><span style="color: #000088;">$lv83</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;t&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'ec'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'hni'</span><span style="color: #339933;">;</span><span style="color: #000088;">$lFa</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'i'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'s '</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">==</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$rx_b_1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span><span style="color: #000088;">$glccUv</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; complic&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$rx_b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'te '</span><span style="color: #339933;">;</span><span style="color: #000088;">$xl1ttf</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'code '</span><span style="color: #339933;">;</span><span style="color: #000088;">$zljal1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;in such a&quot;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$z1b_1</span><span style="color: #339933;">==</span><span style="color: #000088;">$a91</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span><span style="color: #000088;">$p1x2</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; w&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;y &quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$il_7x</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$b1zE_</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'t i'</span><span style="color: #339933;">.</span><span style="color: #000088;">$l1yes</span><span style="color: #339933;">;</span><span style="color: #000088;">$b1zE_</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;i&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$l1yes</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$nltotry_ws</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'st'</span><span style="color: #339933;">.</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$yl5B_</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'thαt '</span><span style="color: #339933;">;</span><span style="color: #000088;">$dlno</span><span style="color: #339933;">=</span><span style="color: #0000ff;">' not '</span><span style="color: #339933;">;</span><span style="color: #000088;">$m1tomanythings</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;under&quot;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$bz_1</span><span style="color: #339933;">=</span><span style="color: #000088;">$s1b_1</span><span style="color: #339933;">;</span><span style="color: #000088;">$Ozaq</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;d&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$bz_1</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;ble&quot;</span><span style="color: #0000ff;">&quot;;echo base64_decode(&quot;</span>JiM4MjIwO09iZnVzY3Rpb24mIzgyMDE7aXMmIzgyMDE7YSYjODIwMTt0ZWNobmlxdWUmIzgyMDE7dXNlZCYjODIwMTt0byYjODIwMWNvbXBsaWNhdGUmIzgyMDE7Y29kZSYjODIwMTtpbiYjODIwMTtzdWNoJiM4MjAxO2EmIzgyMDE7d2F5JiM4MjAxO3RoJmFscGhhO3QmIzgyMDE7aSYjODIwMTt0JiM4MjAxO2kmIzgyMDE7bm90JiM4MjAxO3VuZGVyc3RhbmRhYmxlJnF1b3Q7<span style="color: #0000ff;">&quot;);?&gt;</span></pre></td></tr></table></div>

<p>As you can see, this code is not meant to be distinguishable. There are no distinct variable names, there are no comments, no spacing, no indentation, no distinct order and it’s all in one line.  Even though we cannot distinquish the code, our machines still know what it is. It works.  This one line of chaos simply echos “Obfusction is a technique used to complicate code in such a way that i t i not understandable.”  Yes, I know about the errors.</p>
<p>Obfuscation has pros and cons.  It’s purpose is to disuade a person from finding out what code is doing at a glance, or for a period of time. This is a plus toward individuals with little to no knowledge of the programming language.  However, anybody who has a basic understanding of PHP can disseminate the above obfuscated code and figure out what it’s doing, it might just take a little time.  This is one of the flaws of obfuscation, it is not a form of encryption, it’s just an attempt to be cryptic.  Obfuscation also normally adds to filesize. A lot of the time, you’ll encounter obfuscated code in propriatary and malicious software.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/obfuscation.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/obfuscation.jpg" style="display: inline; "></div>
<p></p>
<h2>So How Can I Obfuscate My Code?</h2>
<p>This is a common question.  There are primarily two ways to obfuscate your code.  First, you can do it by hand.  Writing obfuscated code takes a long time.  The example used in this article took a while to write because of the same reasons you use obfuscation in the first place (lack of structure, order etc…), this even resulted in some menial errors which I didn’t even want to hunt down and fix.  The second way you can obfuscate your code is by buying software that does it for you.  Using a program to obfuscate code is trivial, and of course costs money a lot of the time.  Some software which claims to obfuscate your code, actually encrypts and/or encodes it in such a way it relies on a handshake to function. Often you’ll find software whose vendor won’t even guarantee your code will work when it’s done.  Even in the example, I used a simple <code>Base64 </code>function to encode the construction of the script output.</p>
<p></p>
<h2>Some Obfuscation Tips</h2>
<ul>
<li>Always, always, keep a clean version of the source for yourself.</li>
<li>The more random your technique, the better.</li>
<li>Eliminate all whitespace, where it is not needed.</li>
<li>Character Encode printed/echo’ed characters and spaces (i.e. quotations, thin spaces, apostropes, hypens)
</li>
<li>The more complex the code, the better.</li>
<li>Disregard structure unless it is detrimental to the operation of the code(e.x. variable locations before they’re called)</li>
<li>Do not use distinguishable variable names, namespaces, or class names.</li>
<li>The less code you reuse, the better</li>
<li>Don’t believe it’s foolproof</li>
</ul>
<p></p>
<h2>To Obfuscate or Not to Obfuscate?</h2>
<p>It really depends on your plan. Particularly if your looking to sell your PHP script (or any software) you need to license it.  This is going to be one of the front line defenses to thwart the softwares intended audience from doing whatever they want. However, you may want to obfuscate some, or all of your code for whatever reason. However due to obfuscations negatives, if your really that worried about the security of your source code, it may be worth  looking to encryption instead.</p>
<p></p>
<h2>Cryptography in PHP</h2>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/quoteimage.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/quoteimage.jpg" style="display: inline; "></div>
<p>Wikipedia.com defines cryptography as:</p>
<p style="text-align:center; font-style:italic;">“the practice and study of hiding information.”</p>
<p>Cryptography is a big deal, wether your aware of it or not.  In almost every web application presently deployed there is some presence of cryptography being utilized (i.e. mail clients and websites). As developers we need to be informed and aware of the practical applications of cryptography within our software.  PHP provides us with some very fundamental and practical functions we can utilize to encrypt data.  In this section, I will be mainly going over one-way hashing algorithms though I will touch lightly on Symmetric-key based encryption. There are plenty more (i.e. Steganography, Asymmetric-Key to name a couple).</p>
<p></p>
<h2>The One Way Hash</h2>
<p>Alot of the time we utilize one-way hashing as a way to securely store passwords and check the data integrity of files.  While we do this, to authenticate members of a web application we hash the users entered password, and match it against the users stored hash.  The same technique applies to checking the integrity of files.</p>
<p style="font-weight:bold;">SHA-1, 2, and 3</p>
<p>The SHA family of hash algorithms are currently the most popular, significantly SHA-1.  Even though the SHA-1 algorithm may have a weakness, it is still in wide use.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code219'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123219"><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" id="p123code219"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">///One way hashing with SHA-1</span>
&nbsp;
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sha1"><span style="color: #990000;">sha1</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//or</span>
<span style="color: #000088;">$hash2</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash2</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Will output: 42d2f15c3f92d28d7d58776e5d81b800f662cc6c</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In PHP, SHA-2 is called upon in a different respect, and requires PHP 5 greater than or equal to 5.1.2.  SHA-2 is superior to SHA-1 and can be called with different bit sizes.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code220'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123220"><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
</pre></td><td class="code" id="p123code220"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string_sha256</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string_sha384</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string_sha512</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hash_sha256</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha256'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha256</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash_sha384</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha384'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha384</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash_sha512</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash"><span style="color: #990000;">hash</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha512'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_sha512</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha256</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha384</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hash_sha512</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs repspectively:
sha256 : 09074adc0d70e15b88494643e29c2836e1ab94a21989691dec594cb0bd742ebc
sha384 : 8535470750df54a78701d4bfe0451f9799057a5bc101944a32480d2436e8b95440bce3bcab3f9ce107b0b92d9595ae32
sha512 : c2e6dce873a71800b862791e56b480b976bb26cd3136c02da510c3905caa49b7b9e9260549976e1e741cc93e4569a611f2030d3b7104c6c6c2ff9e6c9bf0946a
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The hash function is called by hash(algorithm, string); In the newest PHP versions the hash() function can be used to call any one-way hash algorithm PHP supports (i.e. md5, sha-1, haval, ghost). If you want to see a list of all the registered hashing algorithms you can use:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code221'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123221"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p123code221"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//As of PHP5 &gt;= 5.1.2</span>
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/hash_algos"><span style="color: #990000;">hash_algos</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>SHA-3 is still being developed and considered for standardization.  A <a href="http://www.csrc.nist.gov/groups/ST/hash/index.html" title="NIST">competition</a> to find a good candidate to act as the new secure hash algorithm was launched by the National Institute of Standards and Technology and entries for the competition were deadlined for October 31, 2008.  A rather popular entry named Skein,  has an available PHP module you can download (though you have to compile it yourself).  Skein is developed by some big names found within the security industry such as Bruce Schneier, Niels Ferguson, and Stefan Lucks to name a few.  The official Skein website can be found <a href="http://www.skein-hash.info" title="Skein Official Site">here</a>.</p>
<div class="tutorial_image"><img src="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/encrypt.jpg" border="0" original="http://d2o0t5hpnwv4c1.cloudfront.net/453_php/encrypt.jpg" style="display: inline; "></div>
<p></p>
<h2>Key-based Encryption</h2>
<p>Symmetric-Key encryption methods is where the security of the encryption primarily resides within a key, which is shared between two points, where the data is encrypted and where the data is decrypted.  A very good example of how this can work was provided by my “<a href="http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/" title="Creating a Crypter Class with PHP">Creating a Crypter Class with PHP</a>” tutorial.</p>
<p></p>
<h2>HMAC</h2>
<p>Essentially HMAC is like a mix between one-way hashing and key based encryption.  HMAC security relies on the key size used, and strength of the hash function it is calculated with.  You can somewhat compare this method to salting passwords.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p123code222'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p123222"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p123code222"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$string_hmac</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GigaSpartan is Awesome&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//hash_hmac(algorithm, string to hash, key)</span>
<span style="color: #000088;">$hmac</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/hash_hmac"><span style="color: #990000;">hash_hmac</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string_hmac</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'secret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$hmac</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></p>
<h2>Wrapping it All Up</h2>
<p>Well what a journey!  Randomizing values, generating random passwords, salting, storing and authenticating users, obfuscation, crypto…seems like alot to take in.  But worth it!  It’s important to know what kind of security your going to implement into your web applications, and how your going to protect it.  Even more, it’s important to keep a clever attitude towards these implementations and not think that security is only implemented by a few methods, but by a combination of them, with a dash of creativity.</p>
]]></content:encoded>
			<wfw:commentRss>http://gigaspartan.com/2010/12/31/tutorial-simple-techniques-to-lock-down-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Creating a Crypter Class with PHP</title>
		<link>http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/</link>
		<comments>http://gigaspartan.com/2010/12/29/tutorial-creating-a-crypter-class-with-php/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 04:02:00 +0000</pubDate>
		<dc:creator>Pat</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[alogorithms]]></category>
		<category><![CDATA[decrypt]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://gigaspartan.com/?p=114</guid>
		<description><![CDATA[In this article I will explain how to create a PHP Class that will encrypt and decrypt any data with a given password. It is object programmed and uses existing PHP algorithms. Introduction Think about what we might need a class like this for? We want to encrypt important data with a password for security [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I will explain how to create a PHP Class that will encrypt and decrypt any data with a given password. It is object programmed and uses existing PHP algorithms.
</p>
<p><span id="more-6941"></span></p>
<p></p>
<h2>Introduction</h2>
<p>Think about what we might need a class like this for? We want to encrypt important data with a password for security reasons. We also want, as already mentioned, to be able to decrypt that data when necessary. Why should you use symmetric algorithms? It’s easy; when you’re offering a password sent via email or something like that, you need the password to be sent in plaintext. The hash algorithms are not reversible. Once you have hashed a string you can’t decipher the original text from the hash. </p>
<p>Maybe you have already heard of MD5? It’s not really the best option anymore because it tends to be unsafe. There are databases around the web – that I don’t want to mention – that can be used to retrieve the plaintext from a hash simply by typing in the hash into a search box. So you should use something like SHA which was developed by the NSA (National Security Agency). SHA is the abbreviation for Secure Hash Algorithm and is one of the most secure hash algorithms. There are some others as well, such as WHIRLPOOL, PANAMA and RIPEMD, but SHA is currently the secure standard for hashes and is used in numerous applications.
</p>
<p><span id="more-114"></span><br />
</p>
<h2><span>Step 1:</span>  Preparation</h2>
<p>I think it is important to create an interface. This is because we can always use the methods which are defined in the interface without thinking, when instancing an object of a class, which implements that interface. </p>
<p>
	When a class implements an interface it has to implement the methods given in that interface, otherwise there will be an error! So here is an example: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code240'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114240"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p114code240"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">interface</span> ICrypter<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">class</span> Crypter implements ICrypter<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
	As you can see, the interface instructs the classes which implement<code> ICrypter</code> to have the public function Encrypt with one parameter <code>$data</code>. The public function Decrypt also has the parameter $<code>data</code>. You can try it out; if the class lacks one of the given methods in the interface, you get a fatal error. Here’s an example:</p>
<blockquote>
<p>
	Fatal error: Class Crypter contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ICrypter::Decrypt) in C:\www\Giga\Crypter\crypter.php on line 32. </p>
</blockquote>
<p>	Nice error right? So you can be sure that the classes really have the methods!
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3317/3619465338_e0b46b5d23_o.jpg" original="http://farm4.static.flickr.com/3317/3619465338_e0b46b5d23_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 2:</span> Password for Encryption and Decryption</h2>
<p>
	As I said before, we want to be able to use a specific password for encryption and decryption. This password has to be accessible for the encrypt- and decrypt-function so we will define an instance variable, called key, which is passed to the constructor. The definition of <code>$Key</code> is only needed in the C<code>rypter</code> Class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code241'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114241"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code241"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Key</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	However, the definition of the constructor has to be in the interface. Therefore, it is also needed in the class, because we have to implement everything we have defined in the interface. The interface will contain: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code242'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114242"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code242"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	and the class: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code243'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114243"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code243"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we know we get a key, we can use it to encrypt and decrypt!
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3595/3619465342_5474e711d2_o.jpg" original="http://farm4.static.flickr.com/3595/3619465342_5474e711d2_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 3:</span> Constructor</h2>
<p>
	In the constructor we have to set the key and choose an algorithm. We will use the Blowfish algorithm for this example and use it as a standard value. I will explain a bit more about the symmetric algorithms later in the text, but for simplicity we will use Blowfish. You can change this later if you want to. So we need another instance variable called Algo: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code244'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114244"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code244"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	and the constructor… </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code245'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114245"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p114code245"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mcrypt_get_key_size"><span style="color: #990000;">mcrypt_get_key_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo <span style="color: #339933;">=</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
	The length of the key depends on the algorithm and the encryption mode. In this example we will use the ECB mode. You can make this variable like we have already done with the algorithm. We use the substring of the given key with the maximum allowed length. You can get this length with the<code> mcrypt_get_key_size </code>function which requires the algorithm and the encryption mode as parameters. </p>
<p>
	Now we give our instance variable Key the correct key for the algorithm and assign our instance variable Algo. </p>
<p>
	So now we have the constructor. As I said previously, you can change the standard value of Algo to any other algorithm that is supported by MCrypt. </p>
<p>
	List of supported algorithms from php.net: </p>
<ul>
<li>MCRYPT_3DES</li>
<li>MCRYPT_ARCFOUR_IV (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_ARCFOUR (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_BLOWFISH</li>
<li>MCRYPT_CAST_128</li>
<li>MCRYPT_CAST_256</li>
<li>MCRYPT_CRYPT</li>
<li>MCRYPT_DES</li>
<li>MCRYPT_DES_COMPAT (libmcrypt 2.2.x only)</li>
<li>MCRYPT_ENIGMA (libmcrypt &gt; 2.4.x only, alias for MCRYPT_CRYPT)</li>
<li>MCRYPT_GOST</li>
<li>MCRYPT_IDEA (non-free)</li>
<li>MCRYPT_LOKI97 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_MARS (libmcrypt &gt; 2.4.x only, non-free)</li>
<li>MCRYPT_PANAMA (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_128 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_192 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RIJNDAEL_256 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RC2</li>
<li>MCRYPT_RC4 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6 (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_RC6_128 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6_192 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_RC6_256 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SAFER64</li>
<li>MCRYPT_SAFER128</li>
<li>MCRYPT_SAFERPLUS (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_SERPENT(libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_SERPENT_128 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SERPENT_192 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SERPENT_256 (libmcrypt 2.2.x only)</li>
<li>MCRYPT_SKIPJACK (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_TEAN (libmcrypt 2.2.x only)</li>
<li>MCRYPT_THREEWAY</li>
<li>MCRYPT_TRIPLEDES (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_TWOFISH (for older mcrypt 2.x versions, or mcrypt &gt; 2.4.x )</li>
<li>MCRYPT_TWOFISH128 (TWOFISHxxx are available in newer 2.x versions, but not in the 2.4.x versions)</li>
<li>MCRYPT_TWOFISH192</li>
<li>MCRYPT_TWOFISH256</li>
<li>MCRYPT_WAKE (libmcrypt &gt; 2.4.x only)</li>
<li>MCRYPT_XTEA (libmcrypt &gt; 2.4.x only)</li>
</ul>
<p>
  	So which one should we use when we want to use the Crypter Class in our products? At the moment AES is the standard of the symmetric algorithms. It is used in many	applications, but where is AES? AES was originally published as Rijndael which is listed. It is a really fast, but secure, algorithm and is even fast with 256-Bit key size. My advice is to use MCRYPT_RIJNDAEL_256 for your applications. Just as an example,	AES is used in WPA2 which is a security standard for WLAN.
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3655/3619465344_c358737684_o.jpg" original="http://farm4.static.flickr.com/3655/3619465344_c358737684_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 4:</span> Now to the Encryption</h2>
<p>
	First thing to check: is there any data to encrypt? If not, you can go ahead and break the encryption. If you want to use any other encryption modes then you have to add the following code. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code246'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114246"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p114code246"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	This <code>$iv</code> is used for example in CBC, CFB, OFB and in some algorithms in STREAM encryption mode. If the parameter is not passed in these modes, the <code>$iv</code> will be set to <code>'\0'</code>. The next step is to encrypt the data with the simple function mcrypt_encrypt. Here we need our algorithm, the key, the data and an encryption mode. $iv is optional. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code247'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114247"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code247"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_encrypt"><span style="color: #990000;">mcrypt_encrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Finally encode the encrypted data with base64_encode and trim it before you return it. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code248'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114248"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code248"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_encode"><span style="color: #990000;">base64_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crypt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	We have to base64 encode the encrypted data to get URL-Safe data. This is needed because, if you want to use the encrypted data, for example in a URL, you will have problems with ‘&amp;’ as it is a reserved character specified in the RFC. So you need something like alphanumeric characters – in other words, character that are safe. The base64 encode supplies these safe characters, which is why we’re using it. We do not know what will be done with the data after encryption. </p>
</p>
<div class="tutorial_image">
	<img border="0" src="http://farm3.static.flickr.com/2470/3619465346_cdf1928555_o.jpg" original="http://farm3.static.flickr.com/2470/3619465346_cdf1928555_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2><span>Step 5:</span> Decryption is Reversed Encryption</h2>
<p>
	Again we ask the same first question. Is there data? If there is, you have to base64_decode the data as we have previously encoded it with base64_encode.  </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code249'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114249"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code249"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Then the optional part with $iv. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code250'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114250"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p114code250"><pre class="php" style="font-family:monospace;">			<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	Decryption with the simple function <code>mcrypt_decrypt</code>. Here we need – nearly – the same parameters. The difference is that the decrypt-function needs to access the crypted data rather than the original data. So here again we use, the algorithm, the key, the crypted data, the encryption mode, and the optional iv. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code251'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114251"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code251"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_decrypt"><span style="color: #990000;">mcrypt_decrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Algo<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Key<span style="color: #339933;">,</span> <span style="color: #000088;">$crypt</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>	Finally return the trimmed and decrypted data. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code252'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114252"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p114code252"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decrypt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<div class="tutorial_image">
	<img border="0" src="http://farm4.static.flickr.com/3339/3619465348_25e097ecf2_o.jpg" original="http://farm4.static.flickr.com/3339/3619465348_25e097ecf2_o.jpg" style="display: inline; ">
</div>
<p></p>
<h2>Examples</h2>
<p>Define a global Crypter. In this example we will use RIJNDAEL_256 (AES) with the password “Any password”. After instancing you call your functions or methods to test it. Here we call the function <code>foo</code> and the method <code>foo1</code>. </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code253'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114253"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p114code253"><pre class="php" style="font-family:monospace;">		<span style="color: #000088;">$crypter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Crypter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Any password&quot;</span><span style="color: #339933;">,</span> MCRYPT_RIJNDAEL_256<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$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: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$foo</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
	You can get your crypter from the Superglobal variable called <code>$GLOBALS</code>. This is an associative array, so you can call all your global variables by the name you defined them with. You can retrieve the <code>$crypter</code> which is defined outside	of the foo or foo1 block with <code>$GLOBALS["crypter"]</code>… </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code254'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114254"><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" id="p114code254"><pre class="php" style="font-family:monospace;">		<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #339933;">...</span>
			<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #339933;">...</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">class</span> Foo<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #339933;">...</span>
				<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$decrypted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;crypter&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #339933;">...</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></p>
<h2>Conclusion</h2>
<p>Here is the final class:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code255'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114255"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p114code255"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> ICrypter<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Crypter implements ICrypter<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Key</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Algo</span> <span style="color: #339933;">=</span> MCRYPT_BLOWFISH<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Key</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mcrypt_get_key_size"><span style="color: #990000;">mcrypt_get_key_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Algo</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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: #339933;">!</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Optional Part, only necessary if you use other encryption mode than ECB</span>
		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_encrypt"><span style="color: #990000;">mcrypt_encrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/base64_encode"><span style="color: #990000;">base64_encode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$crypt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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: #339933;">!</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$crypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/base64_decode"><span style="color: #990000;">base64_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Optional Part, only necessary if you use other encryption mode than ECB</span>
		<span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_get_iv_size"><span style="color: #990000;">mcrypt_get_iv_size</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_create_iv"><span style="color: #990000;">mcrypt_create_iv</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mcrypt_decrypt"><span style="color: #990000;">mcrypt_decrypt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Algo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$crypt</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decrypt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p>And here is another example:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p114code256'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p114256"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p114code256"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'crypter.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Creating a new instance of the crypter with RIJNDAEL_256 encryption</span>
<span style="color: #000088;">$crypter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Crypter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Any password&quot;</span>
