February 26, 2014

February 26, 2014
1
In this article, we are going to discuss about How to create and check the User session using CodeIgniter library. If we create a web application with login (sign in) functionality, then the system must use session to check whether the user is logged in or not. 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. So this article 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');
}
}
}

1 comments: