<?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>Superdit.com &#187; session</title>
	<atom:link href="http://superdit.com/tag/session/feed/" rel="self" type="application/rss+xml" />
	<link>http://superdit.com</link>
	<description>blogging, design, tech, and web</description>
	<lastBuildDate>Thu, 19 Jan 2012 07:50:35 +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>Creating user session checking with CodeIgniter library</title>
		<link>http://superdit.com/2010/04/17/creating-user-session-checking-with-codeigniter-library/</link>
		<comments>http://superdit.com/2010/04/17/creating-user-session-checking-with-codeigniter-library/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 12:19:08 +0000</pubDate>
		<dc:creator>aditia rahman</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://superdit.com/?p=351</guid>
		<description><![CDATA[When we build web application with user authentification, the application must have user login/logout or signin/signout or whatever you called it, then the system must have user logged feature to check whether the user is logged in or logged out. Session is the most common way to checking it. In Codeigniter, using Session Class you<a href="http://superdit.com/2010/04/17/creating-user-session-checking-with-codeigniter-library/" class="read-more">Continue Reading</a>]]></description>
			<content:encoded><![CDATA[<p>When we build web application with user authentification, the application must have user login/logout or signin/signout or whatever you called it, then the system must have user logged feature to check whether the user is logged in or logged out. Session is the most common way to checking it. In <a title="CodeIgniter" href="http://codeigniter.com" target="_blank">Codeigniter</a>, using <a title="CodeIgniter Session Class" href="http://codeigniter.com/user_guide/libraries/sessions.html" target="_blank">Session Class</a> you can use simple function inside controller, it&#8217;s something like this:</p>
<pre class="brush: php; title: ; notranslate">
class User extends Controller {

	// constructor class
	function __construct()
	{
		parent::Controller();
		$this-&gt;load-&gt;library('session');
	}

	function index()
	{
		$this-&gt;load-&gt;view('user/index');
	}

	function login()
	{
		// if user already logged in, redirect to user index
		if ($this-&gt;_is_logged_in())
		{
			redirect('user/index');
		}
		else
		{
			$this-&gt;load-&gt;view('user/login');
		}
	}

	function register()
	{
		// if user already logged in, redirect to user index
		if ($this-&gt;_is_logged_in())
		{
			redirect('user/index');
		}
		else
		{
			$this-&gt;load-&gt;view('user/register');
		}
	}

	// checking user logged user by registered session
	function _is_logged_in()
	{
		$logged = $this-&gt;session-&gt;userdata('user_id');
		if ($logged)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}
</pre>
<p><span id="more-351"></span></p>
<p>The problem using that method is when we have multiple controller using the same function, we have to write the same code again and again, uh that&#8217;s painfull, <a title="Don't Reinvent The Wheel, Unless You Plan on Learning More About Wheels" href="http://www.codinghorror.com/blog/2009/02/dont-reinvent-the-wheel-unless-you-plan-on-learning-more-about-wheels.html" target="_blank">this post have simple answer</a>. Another solution is creating that user session checking function in model class, but when we create a class model in CodeIgniter we must extends CI Model Class and I don&#8217;t think we need to extends the CI Model Class, so I create my own user session library, and here it goes:</p>
<p>Create a php file named <em>MY_Usession.php</em> in folder <em>application/libraries/</em>, Insert this code and save it:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Usession extends CI_Session {

    public $logged_in = FALSE;

    public function  __construct() {
        parent::CI_Session();
        $this-&gt;is_logged_in();
    }

    public function is_logged_in()
    {
        $logged = $this-&gt;userdata('user_id');
        $this-&gt;logged_in = ($logged) ? TRUE : FALSE;
    }
}
</pre>
<p>Open file <em>application/config/autoload.php</em> add the library to the <em>$autoload["libraries"]</em> variable, it may something like this</p>
<pre class="brush: php; title: ; notranslate">
$autoload['libraries'] = array('database', 'session', 'my_usession');
</pre>
<p>Yup now all set, when we load every controller, the MY_Usession library will check whether the user is logged in or not, and you can access it in Controller or in a View by calling it&#8217;s property</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;my_usession-&gt;logged_in
</pre>
<p>For example using in Controller:</p>
<pre class="brush: php; title: ; notranslate">
class User extends Controller {

	// constructor class
	function __construct()
	{
		parent::Controller();
		$this-&gt;load-&gt;library('session');
	}

	function index()
	{
		$this-&gt;load-&gt;view('user/index');
	}

	function login()
	{
		// if user already logged in, redirect to user index
		if ($this-&gt;my_usession-&gt;logged_in)
		{
			redirect('user/index');
		}
		else
		{
			$this-&gt;load-&gt;view('user/login');
		}
	}

	function register()
	{
		// if user already logged in, redirect to user index
		if ($this-&gt;my_usession-&gt;logged_in)
		{
			redirect('user/index');
		}
		else
		{
			$this-&gt;load-&gt;view('user/register');
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://superdit.com/2010/04/17/creating-user-session-checking-with-codeigniter-library/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

