I;m working on a codeigniter project. I want to create a hook so that, it verify the user login every time when he load the class. my Hook
$hook['post_controller_constructor'] = array(
'class' => 'Auth_login',
'function' => 'is_logged_in',
'filename' => 'Auth_login.php',
'filepath' => 'hooks',
);
and also enabled in the config file.
/**
* Auth_login
*/
class Auth_login
{
/**
* loading the CI instance
* @var [object]
*/
private $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->helper('url');
}
/**
* Check the enduser for login verify, every time when the enduser
* loads the controller.
*/
public function is_logged_in()
{
/**
* if end user is not logged in, redirect to the login page
*/
if(!$this->CI->session->userdata('is_logged_in'))
{
$this->CI->session->set_tempdata('faliure','Please sign in to continue');
redirect(site_url('login/index'));
}
}
}
It checks for the session login verify for the end user. If user not logged in then redirect to the login page. Here is my Controller where it redirects
class Login extends CI_Controller {
// class constructor
// public function __construct() {
// parent::__construct();
// // load default
// $this->load->model('login_model');
// }
/**
* [display the login page, and verify the login page using "form_validation" for server side validation]
* @return [redirect] [description]
*/
public function index()
{
$this->load->model('login_model');
// on form sumbit
$on_sumbit = $this->input->post('verify');
if(isset($on_sumbit))
{
// server side validation for login
if ($this->form_validation->run('login/login_verify') == FALSE)
{
$this->load->view('login');
}
else
{
// get the form post value
$user_name = $this->input->post('user_name');
$password = $this->input->post('password');
// verify login
$verified = $this->login_model->login_verify($user_name,$password);
if($verified) // success
{
redirect('dashboard');
}
else // failure
{
$this->session->set_flashdata('login_failure','Please check your email and password and try again');
redirect('login/index');
}
}
}
// login page
$this->load->view('login');
}
// delete active session
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
}
When it redirect to the login controller, it show errors like since it working on the local system "localhost redirected you too many times" what is the actual problem with this. Any help would be appreciated.