I am trying to set-up a 'post_controller_constructor' hook in CodeIgniter. The purpose is to check if a user is not logged in then redirect him back to login page.
Here is my hook configuration array :
$hook['post_controller_constructor'] = array(
'class' => 'Checkpoint',
'function' => 'check_status',
'filename' => 'checkpoint.php',
'filepath' => 'controllers'
);
And this is the class for Hook event
class Checkpoint {
var $CI;
function __construct(){
$this->CI =& get_instance();
}
function check_status() {
if($this->CI->router->class == 'Login'){
return;
}
if (!isset($this->CI->session)){
$this->CI->load->library('session');
}
if(!$this->CI->session->userdata('log_status')){
//redirect(site_url('login'));
echo "not logged in";
}
else{
echo "logged in";
}
}}
Now here are the issues :
When it receives a request from the 'Login' controller it does not return from the first if statement inside check_status() function, and prints 'not logged in' before loading the view.
When I try to redirect if the session userdata is not set it shows an error 'This webpage has a redirect loop' in my browser. For this reason I have commented out the redirection statement
What can I do to solve these issues?