I'm working with sessions for a login function. However every time I attempt log in it just keeps redirecting to the login screen. Below is the code. There is a function set to say that the login is invalid, that is not sending out an error so I'm not sure that it is a login issue.
ATIS Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Atis extends CI_Controller
{
public $status;
public $roles;
function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
$this->is_logged_in();
}
function index(){
$this->load->view("atis/inc/header");
$this->load->view("atis/dashboard");
$this->load->view("atis/inc/footer");
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || is_logged_in !==true)
{
redirect('main/login');
}
}
Main Controller
public function login()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('inc/site_header');
$this->load->view("site_nav");
$this->load->view('login');
$this->load->view('inc/site_footer');
}else{
$post = $this->input->post();
$clean = $this->security->xss_clean($post);
$userInfo = $this->user_model->checkLogin($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'The login was unsucessful');
redirect(site_url().'main/login');
}
foreach($userInfo as $key=>$val){
$this->session->set_userdata($key, $val);
$this->session->set_userdata('is_logged_in');
}
redirect(site_url().'atis/');
}
}
Check login method
public function checkLogin($post)
{
$this->load->library('password');
$this->db->select('*');
$this->db->where('email', $post['email']);
$query = $this->db->get('users');
$userInfo = $query->row();
if(!$this->password->validate_password($post['password'], $userInfo->password)){
error_log('Unsuccessful login attempt('.$post['email'].')');
return false;
}
$this->updateLoginTime($userInfo->id);
unset($userInfo->password);
return $userInfo;
}
Yard Function from ATIS Controller
public function yard()
{
$this->load->model('atisyard_model');
$this->data['yards'] = $this->atisyard_model->getyardname();
$this->data['trackdatabase'] = $this->atisyard_model->gettrains();
$this->load->view("atis/inc/header");
$this->load->view('atis/yard_view', $this->data);
$this->load->view("atis/inc/footer");
}