<?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>Icebergist &#187; PHP</title>
	<atom:link href="http://icebergist.com/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://icebergist.com</link>
	<description>Exploring hidden depths of web apps business</description>
	<lastBuildDate>Mon, 29 Mar 2010 13:14:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple password protected administration with CodeIgniter</title>
		<link>http://icebergist.com/posts/simple-password-protected-administration-with-codeigniter</link>
		<comments>http://icebergist.com/posts/simple-password-protected-administration-with-codeigniter#comments</comments>
		<pubDate>Sun, 08 Mar 2009 17:38:08 +0000</pubDate>
		<dc:creator>Slobodan Kovačević</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[erkanaauth]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[password protected]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icebergist.com/?p=19</guid>
		<description><![CDATA[Last week I&#8217;ve taken a break from Ruby/Rails development and I&#8217;ve worked on a site that uses PHP with CodeIgniter framework.
Despite the fact that CodeIgniter has a very nice documentation I found it very difficult to find a way to do some simple things, that are more or less obvious, but which can be a [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I&#8217;ve taken a break from Ruby/Rails development and I&#8217;ve worked on a site that uses PHP with <a href="http://codeigniter.com/">CodeIgniter</a> framework.</p>
<p>Despite the fact that CodeIgniter has a very nice documentation I found it very difficult to find a way to do some simple things, that are more or less obvious, but which can be a problem for someone who hasn&#8217;t worked with CodeIgniter before. (for example, I found myself more than once looking at CI code to figure out how it works, so I can use it)</p>
<p>I had to make a simple password protected administration section. One admin user, one password, no user registrations, no roles &#8211; simple as possible. As I was using CI framework I decided to find a plugin/library that does this. Unfortunately most CI authorization plugins/libraries are very bloated and too complicated for this simple task. I tried to find some examples how to handle this simple use case, but nothing came up.</p>
<p>Finally I&#8217;ve found a small authorization plugin: <a href="http://codeigniter.com/wiki/Erkana/">Erkanaauth</a>.</p>
<p>First you need a user table (must be named &#8216;users&#8217;) which only needs to have an id field and all other fields are optional because you will manually specify what other columns will be used. I opted for simple id, username, password:</p>
<pre>CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
);</pre>
<p>We will need to &#8220;install&#8221; ErkanaAuth library. You should <a href="http://codeigniter.com/wiki/File:erkanaauth.zip/">download it</a> and unzip it.</p>
<p>Next we should create an Admin controller which will handle all administration tasks (remember I&#8217;m making simple admin here, so I don&#8217;t need to protect multiple controllers).</p>
<pre>&lt;?php
class Admin extends Controller {
  function Admin()
  {
    parent::Controller();
    $this->load->database();
    $this->load->helper(array('url', 'form', 'date'));
    $this->load->library(array('form_validation', 'upload', 'Erkanaauth', 'session'));
  }
}
?&gt;</pre>
<p>Constructor just connects to database and loads some standard helpers and libraries (including Erkanaauth) that are usually used.</p>
<p>Next step is to add a function which we can call to verify if user is logged in:</p>
<pre>
  private
  function authorize()
  {
	  if($this->erkanaauth->try_session_login())
	      return true;

	  redirect('admin/login');
  }
</pre>
<p>Function uses Erkanaauth&#8217;s try_session_login which checks if user is already logged in (checks session for user id). If user isn&#8217;t logged in we&#8217;ll redirect him to our login page:</p>
<pre>
  function login()
  {
    $username = $this->input->post('username', true);
    $password = $this->input->post('password', true);
    if($username || $password)
    {
      if($this->erkanaauth->try_login(array('username' => $username, 'password' => $password)))
        redirect('admin');
    }

    $this->load->view('admin_login');
  }

  function logout()
  {
    $this->erkanaauth->logout();
    redirect('admin');
  }
</pre>
<p>Key command here is try_login in login function which tries to find an entry in users table that fulfills given conditions. If you have different users table than the one I made this is the place where you should enter your column names.</p>
<p>Logout function is has just a simple call to Erkana&#8217;s logout function. Nothing special there.</p>
<p>Of course we also need a login page template which should contain a simple user/pass form. It&#8217;s pretty basic and you can see it if you get the complete code (see at the end).</p>
<p>Finally we have everything needed to protect any page in Admin controller. In order to protect a page all you need to do is to add a call to authorize function to any function you want to protect. Like this:</p>
<pre>
  function index()
  {
    $this->authorize();
    echo "Do something useful... For now just display logout link: ";
    echo anchor('admin/logout', "Logout");
  }
</pre>
<p>That&#8217;s it. Now you have fully functional administration section which requires username and password authorization.</p>
<p>You can get the complete sample application from <a href="http://github.com/basti/ci-admin-section/tree/master">Github repository</a>. Feel free to expand on it or use it any way you like.</p>
<p class="addtoany_share_save">
    <a name="a2a_dd" onmouseover="a2a_show_dropdown(this)" onmouseout="a2a_onMouseOut_delay()" href="http://www.addtoany.com/bookmark?sitename=Icebergist&amp;siteurl=http%3A%2F%2Ficebergist.com%2F&amp;linkname=Simple%20password%20protected%20administration%20with%20CodeIgniter&amp;linkurl=http%3A%2F%2Ficebergist.com%2Fposts%2Fsimple-password-protected-administration-with-codeigniter"><img src="http://icebergist.com/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16"  alt="Share/Save/Bookmark"/></a>
    <script type="text/javascript">
		a2a_linkname="Simple password protected administration with CodeIgniter";
		a2a_linkurl="http://icebergist.com/posts/simple-password-protected-administration-with-codeigniter";
				a2a_show_title=1;    </script>
    <script type="text/javascript" src="http://www.addtoany.com/menu/page.js"></script>

	</p>]]></content:encoded>
			<wfw:commentRss>http://icebergist.com/posts/simple-password-protected-administration-with-codeigniter/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
