Welcome Guest, Not a member yet? Register   Sign In
Issues sending flashdata through my site
#1

[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?
#2

[eluser]Matalina[/eluser]
It's happening because flash data doesn't show up until the page 'reloads'. That's how typical sessions work.

If you set flash data and then load a view it hasn't been retrieved because it already grabbed session data. You need to redirect if you want your users to see the flashdata after you have set it.

You can redirect which is easiest but if you want the form data to remain you need to send the error message to the view.

You can do an easy check so the mark up is not always present:
Code:
if(isset($error)) {
echo $error;
}
#3

[eluser]brianatlarge[/eluser]
Wow, thanks! In my users controller login method I changed

Code:
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');
    }

to

Code:
if($_POST) {
     $this->load->model('Users_model');
     if($this->Users_model->check_login()) {
      redirect('');
     } else {
      $data['error'] = $this->session->flashdata('error');
      redirect('login');
     }
   // If no login information has been sent, display the login form
    } else {
     load_template('login','login');
    }

So redirecting fixed it!




Theme © iAndrew 2016 - Forum software by © MyBB