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');
}
}
}





I’ve been thinking of diving into codeigniter for a while now but after reading your tut it seems pretty interesting!
This is really very helpful Thank you! Buddy
@Superdit very Interesting post, cool, have bookmarked your blog and following your twitter as well.
=Salam dari Batam=
thanks
Excellent post..! really helpful
Dhana
I have used this hint in CI 1.7.3 but in CI 2.0.0 it doesnt work. Can you update your code ? I cant make it work…
Thanks for reply !
WORKING CODE >> FOUND RIGHT CODE
is_logged_in();
}
public function is_logged_in()
{
$logged = $this->userdata(‘user_id’);
$this->logged_in = ($logged) ? TRUE : FALSE;
}
}
this is working
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
Hi Andy,
Thanks for fixing it up,
Lately I haven’t use CI version 2, I will take a look for this, and adding some update on this post
I was looking at this solution, but am having a problem in that when I call the library I believe that the CI_Session is being re-instatiated and a 2nd cookie is being generated.
I thought perhaps the issue was due to .htaccess issues or similar, but have narrowed it down to this library being loaded. Any ideas? Ta.
The session is loading twice, in $autoload and user Controller construct.
Hey! great post!
thanxs!
session is loading twice:
$autoload['libraries'] = array(‘database’, ‘session’, ‘my_usession’); load->library(‘session’); <—-
}
Hi… does it means that we have to add to “each” function of the user class the next lines of code?
if ($this->my_usession->logged_in)
{ …. }
else
{ …. }
or even if we could put that into another function or method we should still have to call that function into “each” function of the user class?
is there any waty to put that validation into the constructor or any other place so that way we just make that validation only one time for each controller instead of making one time for each validation?
I hope you could understand me… please forgive my very bad english
best, regards.
Thanks a lot. this code is help me to run my session.
public function __construct() {
parent::__construct();
$this->is_logged_in();
}