[eluser]brianatlarge[/eluser]
So during my login process, I'd like to send flashdata back for error reporting (incorrect password, etc...).
So I have a users controller with a login method:
Code:
public function login()
{
// If user is logged in, redirect them to the index
if($this->session->userdata('user_email')) {
redirect('');
} else {
// If the login form has been submitted, check to see if the login information is correct
if($this->session->flashdata('error')) {
$data['error'] = $this->session->flashdata('error');
load_template('login','login', $data);
} else {
if($_POST) {
$this->load->model('Users_model');
if($this->Users_model->check_login()) {
redirect('');
} else {
$data['error'] = $this->session->flashdata('error');
load_template('login','login', $data);
}
// If no login information has been sent, display the login form
} else {
load_template('login','login');
}
}
}
}
The checklogin method in my model looks like this:
Code:
function check_login() {
// Check to see if user exists
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('user_email' => $_POST['email']));
$this->db->join('user_meta', 'user_meta.user_id = users.user_id');
$query = $this->db->get();
$num_rows = $query->num_rows();
// If user exists, proceed
if($num_rows != 0) {
// Validate the password
$this->hashed_password = $query->row()->user_password;
if($this->validate_password($_POST['password'])) {
$userdata = array(
'user_email' => $query->row()->user_email,
'user_firstname' => $query->row()->user_meta_firstname,
'user_lastname' => $query->row()->user_meta_lastname
);
$this->session->set_userdata($userdata);
return TRUE;
} else {
$this->session->set_flashdata('error', 'Incorrect Password!');
return FALSE;
}
} else {
$this->session->set_flashdata('error', 'User does not exist!');
return FALSE;
}
}
You may have noticed a load_template function. This is a templating helper I made:
Code:
function load_template($template, $page, $data = '') {
$CI =& get_instance();
if($template == "login") {
$data['title'] = ucfirst($page);
$data['header_includes'] = array(
'css' => 'main_style'
);
$CI->load->view('includes/header', $data);
$CI->load->view('includes/start_container', $data);
if(isset($data['error'])) {
$CI->load->view('includes/error', $data);
}
$CI->load->view($page, $data);
$CI->load->view('includes/stop_container', $data);
$CI->load->view('includes/footer', $data);
}
My error view just echo's what's in the error index.
What happens is if I try to login with a user that doesn't exist, then it redirects back to my login page and loads the error view, but no value is in the error index. If I refresh the page, then the index is populated and shows up like it should.
Why would the error index not have anything in there, and then all of a sudden have the correct data?