0
votes

I have to check out the session in each controller. If user is logged out then redirect to login page. How could i do it using hooks in codeigniter, so that i don't have to write session condition in each controller. Help me out.

I have following code in application/hooks/Authenticate.php

function __construct()
{
    $this->CI =& get_instance();
}


function loginCheck()
{
    if($this->session->userdata('role_id')=='' && $this->session->userdata('user_id')=='' && $this->session->userdata('client_id')==''){ 
    redirect('login');
    }

}

Following code in application/config/hooks.php

$hook['pre_controller'][] = array(
'class' => 'Authenticate',
'function' => 'loginCheck',
'filename' => 'authenticate.php',
'filepath' => 'hooks/authenticate',
'params' => array()
);

When i logout and press browser back button,it again takes me to the login section. It seems that the session is not destroyed properly.

2
What code do you currently have? What have you tried? What isn't working? - gabe3886

2 Answers

0
votes
  1. Create AppController extends CI_Controller in application/core/
  2. Set in config "my_class_prefix" (?) = 'App'
  3. Extends all Application controllers (closed for public access) from AppController
  4. In __construct() of AppController chech session
0
votes

You can check that by making a function for that... check if session is not set then and only then redirect on login...otherwise redirect usr on home/dashboard...

function check_user_login()
{
    if(! $this->session->userdata('logged_in'))
    {
        redirect('login');
    }

    if($this->session->userdata('user_active') == false)
    {
        redirect('your page name');
    }
}