<?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>Phillip Napieralski &#187; PHP</title>
	<atom:link href="http://blog.pnapieralski.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pnapieralski.com</link>
	<description>Programmer, Engineer, Researcher.</description>
	<lastBuildDate>Tue, 20 Dec 2011 16:41:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Migrating from CakePHP 1.2.7 to 1.3</title>
		<link>http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/</link>
		<comments>http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/#comments</comments>
		<pubDate>Fri, 21 May 2010 22:05:50 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=38</guid>
		<description><![CDATA[I decided to migrate one of my in-progress projects to the latest release of CakePHP (1.3). My experience was quick and painless, and perhaps this writing will benefit someone...]]></description>
			<content:encoded><![CDATA[<p>I decided to migrate one of my in-progress projects to the latest release of CakePHP (1.3). My experience was quick and painless, and perhaps this writing will benefit someone.</p>
<h2>Copy/Overwrite</h2>
<p>Copy/overwrite app/config/core.php, in this file change the following</p>
<ul>
<li>Change cipherSeed value to something other than the default</li>
<li>Change the salt value to the same value you had before</li>
</ul>
<p>Copy/overwrite app/webroot/index.php</p>
<p>Lastly, completely copy over the latest cake, plugins and vendors folders. I didn&#8217;t need to do any other modifications to the app folder other than those stated above.</p>
<h2>Add to Code</h2>
<p>Add &#8216;Session&#8217; to the helpers AND components array in app/app_controller.php like so</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class AppController extends Controller {
	var $components = array( ..., 'Session');    // Components used in controllers
	var $helpers = array( ..., 'Session' );         // Helpers used in views
}
?&gt;
</pre>
<p>Also,<br />
If using the <strong>Cookie</strong> Component anywhere, change any reference to del() to delete()</p>
<p>Lastly, $session->flash() no longer auto echoes (this code is probably in your layout). I had to change it like so in my code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
echo $session-&gt;flash();
echo $session-&gt;flash('auth');
?&gt;
</pre>
<h2>Debug Information</h2>
<p>The variable $cakeDebug no longer exists. Instead, use the following code</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php echo $this-&gt;element('sql_dump'); ?&gt;
</pre>
<p>That&#8217;s all I had to do! Everything I mentioned here can be found in the <a href="http://book.cakephp.org/view/1561/Migrating-from-CakePHP-1-2-to-1-3">CakePHP migration guide</a>.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Migrating+from+CakePHP+1.2.7+to+1.3&amp;link=http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/&amp;notes=I%20decided%20to%20migrate%20one%20of%20my%20in-progress%20projects%20to%20the%20latest%20release%20of%20CakePHP%20%281.3%29.%20My%20experience%20was%20quick%20and%20painless%2C%20and%20perhaps%20this%20writing%20will%20benefit%20someone...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/cakephp/migrating-from-cakephp-1-2-to-1-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spam-proof Contact Form with PHP/Akismet</title>
		<link>http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/</link>
		<comments>http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/#comments</comments>
		<pubDate>Sun, 09 May 2010 21:55:20 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Spam]]></category>
		<category><![CDATA[Akismet]]></category>
		<category><![CDATA[Spam Prevention]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=36</guid>
		<description><![CDATA[<a href="http://pnapieralski.com/contact.php">Check out the demo</a>

This is an addition to my previous post to create a <a href="http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/">simple contact form</a>. This post will utilize the <a href="http://akismet.com/">Akismet</a> service to classify some messages as spam.]]></description>
			<content:encoded><![CDATA[<p><a href="http://pnapieralski.com/tuts/isitspam">Check out the demo</a></p>
<p>This is in addition to my previous post about creating a <a href="http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/">simple contact form</a>. This post will utilize the <a href="http://akismet.com/">Akismet</a> service to classify some messages as spam.</p>
<h2>The Prerequisites</h2>
<p>To follow this tutorial, I assume you already have a <a href="http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/">simple contact form</a> in place.</p>
<h2>Akismet PHP Library</h2>
<p>The folks at <a href="http://www.achingbrain.net">Aching Brain</a> created a nifty PHP class to make utilizing the Akismet service easier. <a href="http://www.achingbrain.net/stuff/php/akismet">Download this library</a> then put it in the same directory as your contact form file (Download mirror: <a href='http://blog.pnapieralski.com/wp-content/uploads/2010/05/AkismetPHPClass.zip'>AkismetPHPClass</a>).</p>
<p>There are implementations available in <a href="http://akismet.com/development/">other languages</a> as well.</p>
<h2>The PHP</h2>
<p>This code will completely replace the PHP code from the previous post. However, I assume the HTML used is unchanged.</p>
<p>First, include the Akismet library:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require &quot;Akismet.class.php&quot;;
</pre>
<p>Now, create a more generic function for sending an email:</p>
<pre class="brush: php; title: ; notranslate">
function send_mail( $name, $email, $website, $ip, $is_spam, $message)
  {
  		$subject = '';
  		if( $spam == true )
  			$subject = &quot;[SPAM?]&quot;;
  		$subject .= &quot;[Your_site.com] E-mail received from &quot;.$author_name.&quot;//&quot;.$author_email.&quot;//&quot;.$ip;

  		mail( &quot;send_to_this_address@to.com&quot;, $subject,
		$author_name.&quot;, &quot;.$author_email.&quot;, &quot;.$author_website. &quot;.\r\n\r\n&quot;.$message);
}
</pre>
<p>If the $is_spam parameter is set to true, we simply prepend &#8220;[SPAM?]&#8221; at the beginning of the subject line. This allows us to see right away what it is in our inbox. Further, you could create an <a href="http://mail.google.com/support/bin/answer.py?hl=en&#038;answer=6579">e-mail filter</a> to automatically put these messages in a different e-mail folder.</p>
<p>Now, we have to set-up the Akismet class. This will require a WordPress API Key. If you don&#8217;t have one, <a href="http://en.wordpress.com/api-keys/">it&#8217;s easy and free to get one</a>.</p>
<pre class="brush: php; title: ; notranslate">
	if(isset($_POST['action']))
	{
		$wp_key = 'xXxXxXxXxXxX';
		$our_url = 'http://www.your_website.com';

		$name = $_POST['name'];
		$email = $_POST['email'];
		$website = $_POST['website'];
		$message = $_POST['message'];
		$ip = $_SERVER['REMOTE_ADDR'];

		$akismet = new Akismet($our_url, $wp_key);
		$akismet-&gt;setCommentAuthor($name);
		$akismet-&gt;setCommentAuthorEmail($email);
		$akismet-&gt;setCommentAuthorURL($website);
		$akismet-&gt;setCommentContent($message);
		$akismet-&gt;setUserIP($ip);

		send_mail( $name, $email, $website, $ip, $akismet-&gt;isCommentSpam(), $message);
	}
?&gt;
</pre>
<p>That&#8217;s it! Now if a line of spammers hits your contact form, you have a safe guard against it. If you have questions about this piece of code or anything else, feel free to leave a comment!</p>
<p>In conlusion, Akismet is the best!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Spam-proof+Contact+Form+with+PHP%2FAkismet&amp;link=http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/&amp;notes=Check%20out%20the%20demo%0D%0A%0D%0AThis%20is%20an%20addition%20to%20my%20previous%20post%20to%20create%20a%20simple%20contact%20form.%20This%20post%20will%20utilize%20the%20Akismet%20service%20to%20classify%20some%20messages%20as%20spam.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/spam-proof-contact-form-with-php-akismet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Simple Contact Form with PHP</title>
		<link>http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/</link>
		<comments>http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 01:08:54 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=34</guid>
		<description><![CDATA[This is something that <strong>every</strong> website should have, including my <a href="http://pnapieralski.com/contact" rel="shadowbox">personal portfolio</a>!]]></description>
			<content:encoded><![CDATA[<p>This is something that <strong>every</strong> website should have, including my <a href="http://pnapieralski.com/contact" rel="shadowbox">personal portfolio</a>!</p>
<h2>The HTML</h2>
<pre class="brush: xml; title: ; notranslate">
			&lt;form id=&quot;contact_form&quot; method=&quot;post&quot; action=&quot;contact.php&quot;&gt;
				&lt;input id=&quot;name&quot; type=&quot;text&quot; name=&quot;name&quot; tabindex=&quot;1&quot;/&gt; &lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
				&lt;input id=&quot;email&quot; type=&quot;text&quot; name=&quot;email&quot; tabindex=&quot;2&quot;/&gt; &lt;label for=&quot;email&quot;&gt;E-mail&lt;/label&gt;
				&lt;input id=&quot;website&quot; type=&quot;text&quot; name=&quot;website&quot; tabindex=&quot;3&quot; value=&quot;http://&quot; /&gt; &lt;label for=&quot;website&quot;&gt;Website&lt;/label&gt;
				&lt;textarea id=&quot;message&quot; tabindex=&quot;4&quot; rows=&quot;10&quot; cols=&quot;60&quot; name=&quot;message&quot;&gt;&lt;/textarea&gt;
				&lt;input type=&quot;submit&quot; value=&quot;Send E-mail&quot; tabindex=&quot;5&quot; /&gt;
			&lt;/form&gt;
</pre>
<p>This is just a simple form. I included two things that increase usability quite a bit: Usage of the <strong>label</strong> tag and usage of the tabindex attribute of the various form inputs.</p>
<p>The label tag allows you to click the text next to an input box and have your cursor go into the textbox. You do this by setting the label&#8217;s &#8220;for&#8221; attribute equal to the &#8220;id&#8221; attribute of your input box. Find more information at <a href="http://www.w3schools.com/tags/tag_label.asp">w3schools</a>.</p>
<h2>The PHP</h2>
<pre class="brush: php; title: ; notranslate">
&lt;?php
if( isset($_POST['action']) )
{
		echo 'Thanks for the E-mail!';

		$name = $_POST['name'];
		$email = $_POST['email'];
		$website = $_POST['website'];

		mail( 'send_to_this_address@to.com', &quot;E-mail received from $name, $email, $website&quot;, $_POST['message'] );
}
?&gt;
</pre>
<p>Now, simply put the php code followed by the HTML code into the same php file (I called mine contact.php), and upload it to your webserver!</p>
<p>The php is pretty simple. We grab all the form variables and put them into php&#8217;s mail function. If this function does <strong>not</strong> work, you probably need to <a href="https://help.ubuntu.com/8.04/serverguide/C/postfix.html">install/configure postfix</a>.</p>
<h2>Discussion</h2>
<p>The code is really simple. You can find more information in the <a href="http://php.net/manual/en/function.mail.php">php manual</a> online. A big thing that I did NOT talk about, however, is how to prevent spammers from attacking your contact form. In fact, for a large site it&#8217;s a big problem (my blog gets 10+ spam messages per day).</p>
<p>Luckily! There is a great service available to detect and reject spam messages. It&#8217;s called <a href="http://akismet.com/">Akismet</a>. Expect a blog about this and other ways of protecting against spam in the near future.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Simple+Contact+Form+with+PHP&amp;link=http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/&amp;notes=This%20is%20something%20that%20every%20website%20should%20have%2C%20including%20my%20personal%20portfolio%21&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/simple-contact-us-form-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>User Registration and Login With CakePHP 1.2.x</title>
		<link>http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/</link>
		<comments>http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 11:02:03 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=25</guid>
		<description><![CDATA[I'm going to create a simple user registration form with a confirm password field!

Features of this implementation:
<ul>
	<li>Uses AuthComponent</li>
	<li>Confirm password field</li>
	<li>Ensures unique usernames</li>
	<li>User feedback if any errors occur</li>
	<li>Remember me feature (cookies)</li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to create a simple user registration form with a confirm password field!</p>
<p>Features of this implementation:</p>
<ul>
<li>Uses AuthComponent</li>
<li>Confirm password field</li>
<li>Ensures unique usernames</li>
<li>User feedback if any errors occur</li>
<li>Remember me feature (cookies)</li>
</ul>
<h2>Prerequisites</h2>
<p>I assume you have a database structure similar to the following:</p>
<div id="attachment_26" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/02/asdf.png" rel="shadowbox[sbpost-25];player=img;"><img class="size-medium wp-image-26" title="Database Layout of Users and Groups Tables" src="http://blog.pnapieralski.com/wp-content/uploads/2010/02/asdf-300x154.png" alt="Users and Groups tables" width="300" height="154" /></a><p class="wp-caption-text"> </p></div>
<p>Note: CakePHP automagically updates the created and modified fields.</p>
<h2>App Controller</h2>
<p>Place the following code in <em>/app/controllers/app_controller.php</em></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

class AppController extends Controller {
	var $components = array( 'Auth', 'RememberMe' );

	function beforeFilter() {
		$this-&gt;Auth-&gt;logoutRedirect =  '/';
		$this-&gt;Auth-&gt;loginRedirect = '/';
		$this-&gt;RememberMe-&gt;check();
	}
}
?&gt;
</pre>
<p>The two lines $this-&gt;Auth-&gt;log*** determine where the user is directed after they login. Another option (instead of &#8216;/&#8217;), is <em>$this-&gt;referer()</em>. That will bring the user back to the page they were just at (sweet)!</p>
<p>What&#8217;s this remember me crap though?</p>
<h2>Remember Me</h2>
<p><a href="http://dsi.vozibrale.com/articles/view/rememberme-component-for-cakephp">This guy</a> wrote an excellent component that you can simply pop in your <em>/app/controllers/components</em> folder and you&#8217;re ready to go.</p>
<h2>The Form</h2>
<p>Place the following code in <em>/app/views/users/register.ctp</em></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
	echo $form-&gt;create('User', array( 'url' =&gt; array( 'action' =&gt; 'register' ) ) );
	echo $form-&gt;input('username',
					 	array('after' =&gt; $form-&gt;error('username_unique', 'The username is taken. Do try again!' ) ) );
	echo $form-&gt;input('password');
	echo $form-&gt;input('confirm_password', array( 'type' =&gt; 'password' ) );
	echo $form-&gt;input('email');
	echo $form-&gt;end('Sign-up');
?&gt;
</pre>
<p>The only tricky piece right now should be in the username piece. In the second parameter, I specified &#8216;after&#8217; that associates with a $form-&gt;error(&#8230;). The username_unique property is something I will define in the User Model shortly.</p>
<h2>Users Controller</h2>
<p>Place the following code in <em>/app/controllers/users_controller.php</em> <strong>inside</strong> your UsersController class.</p>
<pre class="brush: php; title: ; notranslate">
	function beforeFilter()
	{
		parent::beforeFilter();

		// This allows our login() function to execute
		$this-&gt;Auth-&gt;autoRedirect = false;
	}

	// Note, this function is called AFTER Auth has handled it, but BEFORE redirecting to the afterLogin redirect specified (because of the flag we set in beforeFilter)
	// Basically, this means the password field is already encrypted and the session was already created. Easy! =)
	function login()
	{
		// Check if Auth got a hold of it and created a session
		if( $this-&gt;Auth-&gt;user() )
		{
			// If form data was sent, set the remember_me cookie IF they checked the box
			if( isset( $this-&gt;data ) )
			{
				if( empty( $this-&gt;data['User']['remember_me'] ) || $this-&gt;data['User']['remember_me'] == 0 )
				{
					$this-&gt;RememberMe-&gt;delete();
				}
				else
				{
					$this-&gt;RememberMe-&gt;remember(
								$this-&gt;data['User']['username'],
								$this-&gt;data['User']['password'] );
				}

				$this-&gt;Session-&gt;setFlash('You are logged in!');
			}

			// Redirect to our loginRedirect page specified
			$this-&gt;redirect($this-&gt;Auth-&gt;redirect());
		}
	}

	function logout(){
		// Set logout message and call auth component's logout
		$this-&gt;Session-&gt;setFlash('You are logged out! Good bye!');

		// Delete the cookie
		$this-&gt;RememberMe-&gt;delete();

		// Call Auth to delete our session
		$this-&gt;redirect($this-&gt;Auth-&gt;logout());
	}

	function register() {
		if ( isset($this-&gt;data) )
		{
			// Set group to regular user
			//    NOTE: This may be different for you
			$this-&gt;data['User']['group_id'] = 2;

			$this-&gt;User-&gt;create();
			if ($this-&gt;User-&gt;save($this-&gt;data))
			{
				$this-&gt;Session-&gt;setFlash( 'Thank you for registering!' );
				$this-&gt;redirect(array('action'=&gt;'index'));
			}
			else
			{
				// Make the password fields blank
				unset($this-&gt;data['User']['password']);
				unset($this-&gt;data['User']['confirm_password']);

				$this-&gt;Session-&gt;setFlash('An error occurred, try again!');
			}
		}
	}
</pre>
<p>Excellent! Now we just have one more step&#8230; let&#8217;s validate the user&#8217;s input.</p>
<h2>The User Model</h2>
<p>Place the following code in <em>/app/models/user.php</em></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class User extends AppModel {

	var $name = 'User';

	// Prevent the input from having an id
	// Username: MUST be alphanumeric
	// Password: We specify a function that returns a bool value
	var $validate = array(
		'id' =&gt; array(
							'rule' =&gt; 'blank',
							'on' =&gt; 'create'
					 ),
		'username' =&gt; array(
							'rule' =&gt; 'alphanumeric',
							'message' =&gt; 'Please enter a valid username',
							'required' =&gt; true
						),
		'password' =&gt; array(
							'rule' =&gt; array('confirmPassword', 'password'),
							'message' =&gt; 'Passwords do not match',
							'required' =&gt; 'true'
						),
		'confirm_password' =&gt; array(
							'rule' =&gt; 'alphanumeric',
							'required' =&gt; 'true'
						),
		'email' =&gt; array( 'email',
							array( 'rule' =&gt; array('email'),
									'message' =&gt; 'Please enter a valid email!' )
						),
		'group_id' =&gt; array( 'numeric' )
	);

	function confirmPassword($data)
	{
		// We must manually hash the second piece in the same way the AuthComponent would
		// if they match, return true!
		if ($data['password'] == Security::hash(Configure::read('Security.salt') . $this-&gt;data['User']['confirm_password'])) {
			return true;
		}

		// hashed passwords did NOT match
		return false;
	}

	// Check if the username already exists by doing SELECT COUNT(*) FROM users WHERE username = 'your_username'
	function beforeValidate()
	{
		if( !$this-&gt;id )
		{
			if( $this-&gt;findCount( array('User.username' =&gt; $this-&gt;data['User']['username'] ) ) &gt; 0 )
			{
				// If any rows are found, send an error and call it 'username_unique'
				// In our view, we can check for this by doing $form-&gt;error('username_unique','Not Unique Username!!!')
				//   As specified in the view code I placed above
				$this-&gt;invalidate('username_unique');
				return false;
			}
		}
		return true;
	}

	// Associations
	var $belongsTo = 'Group';
}
?&gt;
</pre>
<p>Great!</p>
<h2>Conclusion</h2>
<p>There you have it. Something that may take you places in your projects (hopefully). Any questions?</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=User+Registration+and+Login+With+CakePHP+1.2.x&amp;link=http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/&amp;notes=I%27m%20going%20to%20create%20a%20simple%20user%20registration%20form%20with%20a%20confirm%20password%20field%21%0D%0A%0D%0AFeatures%20of%20this%20implementation%3A%0D%0A%0D%0A%09Uses%20AuthComponent%0D%0A%09Confirm%20password%20field%0D%0A%09Ensures%20unique%20usernames%0D%0A%09User%20feedback%20if%20any%20errors%20occur%0D%0A%09Remember%20me%20feature%20%28cookies%29%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/cakephp/user-registration-and-login-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP Cheatsheets</title>
		<link>http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/</link>
		<comments>http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 23:32:11 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=23</guid>
		<description><![CDATA[This is a list of cheatsheets and links for things that I commonly use (and forget the syntax to) in CakePHP.]]></description>
			<content:encoded><![CDATA[<p>This is a list of cheatsheets and links for things that I commonly use (and forget the syntax to) in CakePHP.</p>
<ul>
<li><a href="http://cakephp.org/files/Resources/CakePHP-1.2-Cheatsheet.pdf">Official Cake Cheatsheet</a></li>
<li><a href="http://book.cakephp.org/view/205/HTML">HtmlHelper</a></li>
<li><a href="http://book.cakephp.org/view/182/form">FormHelper</a></li>
<li><a href="http://book.cakephp.org/view/217/Time">TimeHelper</a></li>
<li><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/02/cakephp1.2-model-associations-1-1.pdf">Model Associations</a></li>
<li><a title="Retrieving Data - Find" href="http://book.cakephp.org/view/73/Retrieving-Your-Data">Retrieving Data &#8211; Find</a></li>
<li><a title="Find between CakePHP" href="http://devdaily.com/php/cakephp-find-between-select-query-syntax">Retrieving Data &#8211; More Find stuff</a></li>
<li><a href="http://book.cakephp.org/view/165/Controller-Setup">Pagination</a></li>
</ul>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=CakePHP+Cheatsheets&amp;link=http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/&amp;notes=This%20is%20a%20list%20of%20cheatsheets%20and%20links%20for%20things%20that%20I%20commonly%20use%20%28and%20forget%20the%20syntax%20to%29%20in%20CakePHP.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/cakephp/cakephp-cheatsheets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Very Simple User Authentication with CakePHP</title>
		<link>http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/</link>
		<comments>http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 08:56:10 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Software Engineering II]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=22</guid>
		<description><![CDATA[I'm working with my Software Engineering II class to build an online survey for United Way. I was left in charge to create a simple way to login and authenticate.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working with my <a href="http://blog.pnapieralski.com/servers/software-engineering-ii-part-1/">Software Engineering II class</a> to build an online survey for United Way. I was left in charge to create a simple way to login and authenticate.</p>
<p>The following implementation uses sessions exclusively. I built it so you MUST log-in before accessing any part of the site/survey. You could easily build off this, or even switch to Cake&#8217;s built-in <a href="http://book.cakephp.org/view/170/Core-Components">ACL and Auth</a> features.</p>
<h2>Preliminaries</h2>
<p>The database structure presented is the same as in <a href="http://blog.pnapieralski.com/php/user-account-class-in-php-1/">my previous post</a> about a user account class. Here it is again however:</p>
<div id="attachment_15" class="wp-caption alignnone" style="width: 280px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/02/usergroup.png" rel="shadowbox[sbpost-22];player=img;"><img class="size-medium wp-image-15  " title="usergroup" src="http://blog.pnapieralski.com/wp-content/uploads/2010/02/usergroup-300x219.png" alt="" width="270" height="197" /></a><p class="wp-caption-text"> </p></div>
<h2>The Login Form View</h2>
<p>The login form is extremely simple using Cake&#8217;s built in form helper. Here&#8217;s how we do it:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
// Pass the model name into the Create function, also pass where the data will be sent
echo $form-&gt;create('User', array( 'controller' =&gt; 'users', 'action' =&gt; 'login' ) );

// Cake automatically knows, based on the input, to create input fields for these two. Cool eh?
echo $form-&gt;input('username');
echo $form-&gt;input('password');

// Create the submit button
echo $form-&gt;end('Login');
?&gt;
</pre>
<p>That&#8217;s much less code, and way clearer once you get used to it. I placed this file in <em>/app/views/users/login_form.ctp</em></p>
<h2>The User Model</h2>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class User extends AppModel {
	function validateLogin($data)
	{
		// Search our database where the 'username' field is equal to our form input.
		// Same with the password (this example uses PLAIN TEXT passwords, you should encrypt yours!)
		// The second parameter tells us which fields to return from the database
		// Here is the corresponding query:
		// &quot;SELECT id, username FROM users WHERE username = 'xxx' AND password = 'yyy'&quot;
		$user = $this-&gt;find(array('username' =&gt; $data['username'], 'password' =&gt; $data['password']), array('id', 'username'));

		if( empty($user) == false )
		{
			return $user;
		}

		return false;
	}
}
?&gt;
</pre>
<p>This code should go in <em>/app/models/user.php</em></p>
<h2>The Users Controller</h2>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class UsersController extends AppController {
	var $name = 'Users';
	var $helpers = array('Form');

	// Placeholder for login_form, required by CakePHP to see the login_form view
	function login_form() { }

	function login() {
		// Check if they went here after submitting the form
		// Note that all our form data is preceded by the model name ['User']
		if(empty($this-&gt;data['User']['username']) == false)
		{
			// Here we validate the user by calling that method from the User model
			if(($user = $this-&gt;User-&gt;validateLogin($this-&gt;data['User'])) != false)
			{
				// Write some Session variables and redirect to our next page!
				$this-&gt;Session-&gt;setFlash('Thank you for logging in!');
				$this-&gt;Session-&gt;write('User', $user);

				// Go to our first destination!
				$this-&gt;Redirect(array('controller' =&gt; 'Controller_name', 'action' =&gt; 'Action_name'));
				exit();
			}
			else
			{
				$this-&gt;Session-&gt;setFlash('Incorrect username/password!', true);
				$this-&gt;Redirect(array('action' =&gt; 'login_form'));
				exit();
			}
		}
	}

	function logout() {

		$this-&gt;Session-&gt;destroy();
		$this-&gt;Session-&gt;setFlash('You have been logged out!');

		// Go home!
		$this-&gt;Redirect('/');
		exit();
	}
}
?&gt;
</pre>
<p>Place this code in <em>/app/controllers/users_controller.php</em></p>
<h2>The AppController &#8211; Validate on Every Page</h2>
<pre class="brush: php; title: ; notranslate">
class AppController extends Controller {

	// Check if they are logged in
	function authenticate()
	{
		// Check if the session variable User exists, redirect to loginform if not
		if(!$this-&gt;Session-&gt;check('User'))
		{
			$this-&gt;redirect(array('controller' =&gt; 'users', 'action' =&gt; 'login_form'));
			exit();
		}
	}

	// Authenticate on every action, except the login form
	function afterFilter()
	{
		if( $this-&gt;action != 'login_form' )
		{
			$this-&gt;authenticate();
		}
	}
}
</pre>
<p>Now simply place this code in <em>/app/app_controller.php</em></p>
<p>What does this do? Well, now no matter where anyone goes on your website, they will be redirected to the login page UNLESS they are logged in! Excellent.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Very+Simple+User+Authentication+with+CakePHP&amp;link=http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/&amp;notes=I%27m%20working%20with%20my%20Software%20Engineering%20II%20class%20to%20build%20an%20online%20survey%20for%20United%20Way.%20I%20was%20left%20in%20charge%20to%20create%20a%20simple%20way%20to%20login%20and%20authenticate.&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/cakephp/very-simple-user-authentication-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>User Account Class in PHP 2</title>
		<link>http://blog.pnapieralski.com/php/user-account-class-in-php-2/</link>
		<comments>http://blog.pnapieralski.com/php/user-account-class-in-php-2/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 16:10:00 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[mysqli]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Happy Polack]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=18</guid>
		<description><![CDATA[I started up an account class in my previous post, but I left out a way for the user to authenticate! Let's fix that...]]></description>
			<content:encoded><![CDATA[<p>I started up an account class in my previous post, but I left out a way for the user to authenticate! Let&#8217;s fix that&#8230;</p>
<h2>Authenticate</h2>
<pre class="brush: php; title: ; notranslate">
	public function authenticate($group_id = 0) {
		$dbconn = @Database::grab();

		// They must have ALL three cookies set from the login function, otherwise something went wrong!
		if( !isset($_COOKIE['id']) &amp;&amp; !isset($_COOKIE['cookie']) &amp;&amp; !isset($_COOKIE['name']) )
		{
			return false;
		}

		// Sanitize
		$id = safe($dbconn, $_COOKIE['id']);
		$cookie = safe($dbconn, $_COOKIE['cookie']);
		$username = safe($dbconn, $_COOKIE['name']);

		// Make sure they have a valid cookie value first! (64 characters long in our case)
		if( strlen($cookie) &gt; 63 )
		{
			// Check the values in the DB with a quick query
			$query = &quot;SELECT id, username, cookie, group_id FROM users WHERE
					id = &quot;.$id.&quot; AND
					cookie = '&quot;.$cookie.&quot;' AND username = '&quot;.$username.&quot;' AND group_id &gt;= $group_id LIMIT 1&quot;;
			$queryData = $dbconn-&gt;query($query);
		}
		if( $queryData )
		{
			if( $queryData-&gt;num_rows &gt; 0) {
				$data = $queryData-&gt;fetch_object();
				$this-&gt;group_id = $data-&gt;group_id;
				$this-&gt;user_id = $data-&gt;id;
				$this-&gt;username = $data-&gt;username;
				return true;
			}
		}

		// They failed to authenticate! Wrong username/pass?
		$this-&gt;logout();
		return false;
	}
</pre>
<p>There you have it. Basically, check to make sure they have all the cookies you set in your login function. If any are missing, fail. Then, we write a query to check the credentials from the cookies to see if they match what&#8217;s in the database. If not, clean up their cookies with $this->logout() and return false. That reminds me, here&#8217;s the code for logout:</p>
<h2>Logout</h2>
<pre class="brush: php; title: ; notranslate">
	public function logout() {
		$dbconn = @Database::grab();

		if(isset($_COOKIE[&quot;id&quot;]) &amp;&amp; isset($_COOKIE[&quot;cookie&quot;]))
		{
			$id = safe($dbconn, $_COOKIE['id']);
			$cookie = safe($dbconn, $_COOKIE['cookie']);

			// Set the cookie to null in the database
			$query = &quot;UPDATE users SET cookie = 0 WHERE id = &quot;.$id.&quot; AND cookie = '&quot;.$cookie.&quot;'&quot;;
			$dbconn-&gt;query($query);
		}
		// Expire our cookies by setting the time to 1 second after the EPOCH
		setcookie(&quot;hpname&quot;, &quot;&quot;, 1, &quot;/&quot; );
		setcookie(&quot;hpid&quot;, &quot;&quot;, 1,&quot;/&quot; );
		setcookie(&quot;hpcookie&quot;, &quot;&quot;, 1,&quot;/&quot; );

		// Unset them immediately
		unset($_COOKIE['hpname']);
		unset($_COOKIE['hpid']);
		unset($_COOKIE['hpcookie']);
	}
</pre>
<p>There you go. What&#8217;s missing now? We just need a forgot password function and we&#8217;re good to go.</p>
<p>Lost on this tutorial? Try going back to <a href="http://blog.pnapieralski.com/php/user-account-class-in-php-1/">part 1</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+2&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-2/&amp;notes=I%20started%20up%20an%20account%20class%20in%20my%20previous%20post%2C%20but%20I%20left%20out%20a%20way%20for%20the%20user%20to%20authenticate%21%20Let%27s%20fix%20that...&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/user-account-class-in-php-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>User Account Class in PHP 1</title>
		<link>http://blog.pnapieralski.com/php/user-account-class-in-php-1/</link>
		<comments>http://blog.pnapieralski.com/php/user-account-class-in-php-1/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 07:29:48 +0000</pubDate>
		<dc:creator>Phillip Napieralski</dc:creator>
				<category><![CDATA[mysqli]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Happy Polack]]></category>

		<guid isPermaLink="false">http://blog.pnapieralski.com/?p=14</guid>
		<description><![CDATA[I'm going to describe the class I created to handle signup/login/authentication at happypolack.com.

If you'd like a demo, <a title="Happy Polack" href="http://happypolack.com">create an account at Happy Polack :)</a>.

The features in this class are:
<ol>
<li>Signing up with confirmation e-mail</li>
<li>Logging in/out</li>
<li>Authenticate/differentiate regular user and administrators</li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to describe the class I created to handle signup/login/authentication at happypolack.com.</p>
<p>If you&#8217;d like a demo, <a title="Happy Polack" href="http://happypolack.com">create an account at Happy Polack <img src='http://blog.pnapieralski.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a>.</p>
<h2>The Database Structure</h2>
<p>First, what is it going to look like in the database? Well, here&#8217;s what I used:</p>
<div id="attachment_15" class="wp-caption alignnone" style="width: 365px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/02/usergroup.png" rel="shadowbox[sbpost-14];player=img;"><img class="size-full wp-image-15" title="usergroup" src="http://blog.pnapieralski.com/wp-content/uploads/2010/02/usergroup.png" alt="" width="355" height="260" /></a><p class="wp-caption-text">Generated using MySQL Workbench</p></div>
<p>Take note of the indexes used. I have a foreign key relating the users and the groups table (1-1 for now). I also have a unique index on the username to prevent the same username. Lastly, I have a regular index on the email address to make searching that field faster (useful if they forgot their password).</p>
<p>The other thing to note is that both <em>password</em> and <em>cookie</em> have a datatype of CHAR(64). This is because I encrypted the password using a SHA256 algorithm, which is WAY stronger than md5, before placing it in the database (this is PHPs <em>hash(&#8220;sha256&#8243;,$unenc_password)</em> function). For the cookie field, I use that algorithm on the uniq_id() function built into PHP. I&#8217;ll get to that in a minute.</p>
<h2>The Class Structure</h2>
<pre class="brush: php; title: ; notranslate">
class AccountController {
        // Initialize variables to something arbitrary
	private $group_id = -1;
	private $user_id = -1;
	private $username = '';

	public function signup($username, $password, $email){...}
	public function login($username, $password){...}
	public function logout(){...}
	public function authenticate($group_id = 0){...}
	public function is_logged_in(){...}
	public function is_admin(){...}
}
</pre>
<p>Excellent! We got the basic functionality out of the way. We are able to signup/login/logout/authenticate. We can even pass a group_id to authenticate to make sure they are part of a specific group.</p>
<h3>Signup</h3>
<pre class="brush: php; title: ; notranslate">
public function signup($username, $password, $email) {
	$dbconn = @Database::grab(); // This WILL be different for you. I use a simple MySQLi wrapper.

	$origpassword =  $password; // store original password to send to them
	$password = hash('sha256', $password);
	if(!$dbconn-&gt;query($query))
		return false;
	else
	{
		// Send a nice email
		$message = '&lt;b&gt;Account Information:&lt;/b&gt;';
		$message .= '&lt;p&gt;Username: '.$username;
		$message .= '&lt;/p&gt;&lt;p&gt;Password: '.$origpassword;
		$message .= '&lt;/p&gt;&lt;p&gt;Your profile: http://happypolack.com/user/'.$username.'&lt;/p&gt;;

		send_mail( $email, &quot;Welcome to Happy Polack!&quot;, &quot;Hello and Welcome to Happy Polack!&quot;, $message );
	}
	return true;
}
</pre>
<p>There you have it! Now we can signup, well&#8230; except for the send_mail piece, let me give you the code for that:</p>
<pre class="brush: php; title: ; notranslate">
function send_mail($to, $subject, $title, $msg)
{
	$headers = 'From: Happy Polack &lt;you@example.com&gt;'. &quot;\r\n&quot;.
			'Reply-To: you@example.com' . &quot;\r\n&quot; .
			'X-Mailer: PHP/' .phpversion(). &quot;\r\n&quot;;

	$headers .= 'MIME-Version: 1.0' . &quot;\r\n&quot;;
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . &quot;\r\n&quot;;

	$message =  '&lt;h1&gt;'. $title.'&lt;/h1&gt;';
	$message .= $msg;

	mail( $to, $subject, $message, $headers);
}
</pre>
<p>Basically, this nifty function automatically sets up the headers so we can use HTML in our e-mails and it automatically formats various features (this implementation could be spruced up with some neat css too).</p>
<h3>Login</h3>
<pre class="brush: php; title: ; notranslate">
public function login($username, $password) {
	$dbconn = @Database::grab();

	$epassword = hash(&quot;sha256&quot;,$password); // Encrypt using SHA256. We could also &quot;salt&quot; the password before hashing for added security.

	$query = &quot;SELECT id, group_id FROM users WHERE username = '$username' AND password = '$epassword' LIMIT 1&quot;;
	$qData = $dbconn-&gt;query($query) ;
	if( $qData-&gt;num_rows &gt; 0 )
	{
		$userData = $qData-&gt;fetch_array();
		$cookie = generate_token();
		$userID = $userData['id'];

		$query = &quot;UPDATE users SET cookie = '$cookie' WHERE username = '$username' AND password = '$epassword'&quot;;

		if($dbconn-&gt;query($Query))
		{
			$this-&gt;user_id = $userID;
			$this-&gt;group_id = $userData['group_id'];
			$expire_time = 60*60*24*7+time(); // About one week
			// When we create the cookies, we pass the cookie's name, the value, the expire time and what piece of the site the cookie is valid on. I specify &quot;/&quot; to allow it to be usable throughout the whole website.
			setcookie(&quot;id&quot;, $userID, $expire_time, &quot;/&quot;);
			setcookie(&quot;cookie&quot;, $cookie, $expire_time, &quot;/&quot;);
			setcookie(&quot;username&quot;, $username, $expire_time, &quot;/&quot;);

			// This piece is so that the COOKIE values are set immediately. When I left this piece out, I noticed it would take an extra page load for the cookies to register. This is because the cookies are generally sent with everyday headers and *it seems* that they are not processed right away.
			$_COOKIE['id'] = $userID;
			$_COOKIE['cookie'] = $cookie;
			$_COOKIE['username'] = $username;

			return true; // SUCCESS, now logged in!
		}
	}
	return false; // Bummer!
}
</pre>
<p>What&#8217;s happening here? Well, let&#8217;s get a pretty picture to clarify the order a little bit:</p>
<div id="attachment_16" class="wp-caption alignnone" style="width: 192px"><a href="http://blog.pnapieralski.com/wp-content/uploads/2010/02/74dc5b69.png" rel="shadowbox[sbpost-14];player=img;"><img class="size-full wp-image-16   " title="74dc5b69" src="http://blog.pnapieralski.com/wp-content/uploads/2010/02/74dc5b69.png" alt="" width="182" height="358" /></a><p class="wp-caption-text">Generated with yUML - Does this help?</p></div>
<p>In easier terms: Check if the user is in the database -> if he is, put a random value into his <em>cookie</em> field for later authentication and set his cookies.</p>
<p>That reminds me, here is one last function, <em>generate_token()</em>, that is pretty essential, and simple, to get a random string of length 64.</p>
<pre class="brush: php; title: ; notranslate">
// Return a random string of length 64
function generate_token() {
	return hash('sha256', uniqid(mt_rand(), true));
}
</pre>
<h2>Next Time</h2>
<p>Wow! Progress has been made. But there&#8217;s more to be had. We still need an authenticate function as well as a way to logout!</p>
<p>Phew.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=User+Account+Class+in+PHP+1&amp;link=http://blog.pnapieralski.com/php/user-account-class-in-php-1/&amp;notes=I%27m%20going%20to%20describe%20the%20class%20I%20created%20to%20handle%20signup%2Flogin%2Fauthentication%20at%20happypolack.com.%0D%0A%0D%0AIf%20you%27d%20like%20a%20demo%2C%20create%20an%20account%20at%20Happy%20Polack%20%3A%29.%0D%0A%0D%0AThe%20features%20in%20this%20class%20are%3A%0D%0A%0D%0ASigning%20up%20with%20confirmation%20e-mail%0D%0ALogging%20in%2Fout%0D%0AAuthenticate%2Fdifferentiate%20regular%20user%20and%20administrators%0D%0A&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.pnapieralski.com/php/user-account-class-in-php-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

