Creating user session checking with CodeIgniter library

April 17th, 2010 by aditia rahman / 17 Comments  

     

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 can use simple function inside controller, it’s something like this:

class User extends Controller {

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

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

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

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

	// checking user logged user by registered session
	function _is_logged_in()
	{
		$logged = $this->session->userdata('user_id');
		if ($logged)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}

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’s painfull, this post have simple answer. 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’t think we need to extends the CI Model Class, so I create my own user session library, and here it goes:

Create a php file named MY_Usession.php in folder application/libraries/, Insert this code and save it:

<?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->is_logged_in();
    }

    public function is_logged_in()
    {
        $logged = $this->userdata('user_id');
        $this->logged_in = ($logged) ? TRUE : FALSE;
    }
}

Open file application/config/autoload.php add the library to the $autoload["libraries"] variable, it may something like this

$autoload['libraries'] = array('database', 'session', 'my_usession');

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’s property

$this->my_usession->logged_in

For example using in Controller:

class User extends Controller {

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

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

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

	function register()
	{
		// if user already logged in, redirect to user index
		if ($this->my_usession->logged_in)
		{
			redirect('user/index');
		}
		else
		{
			$this->load->view('user/register');
		}
	}
}
        submit to reddit Delicious

17 Comments Leave a Comment Subscribe RSS

Leave a Comment