<?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; Software Engineering II</title>
	<atom:link href="http://blog.pnapieralski.com/tag/software-engineering-ii/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>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>
	</channel>
</rss>

