Welcome Guest, Not a member yet? Register   Sign In
if statement not working in codeigniter
#1

[eluser]ede196620[/eluser]
Im building a log in for my website I have this if statement:

Code:
$application = 'home';
    $this->user_model->loggedin() == TRUE || redirect($application);


    $rules = $this->user_model->_rules;
    $this->form_validation->set_rules($rules);
    if ($this->form_validation->run() !== FALSE) {
          if ($this->user_model->login() !== FALSE) {
            redirect('home');
            echo 'success';
        }  else {
           echo 'fail';
        }

    }
For some reason it dose not run the else when I present with the condition == FALSE which would be entering wrong email and password. either way i get the message success am I missing something here ?

here is the function login in user_model if it helps, cant figure this one out any help will be appreciated.

Code:
public function login() {
    $user = $this->get_by(array(
        'email_address' => $this->input->post('email_address'),
        'password' => $this->hash($this->input->post('password'))
            ), TRUE);

    if (count($user)) {
        // log in user
        $data = array(
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'email_address' => $user->email_address,
            'id' => $user->id,
            'loggedin' => TRUE,
        );
      $this->session->set_userdata($data);  
    }

}
#2

[eluser]Tim Brownlaw[/eluser]
Hi,

Well your call to $this->user_model->login() doesn't return anything...

As a quick guess, and you'll need to verify this, your model could be this...

Code:
public function login() {
  $user = $this->get_by(array(
    'email_address' => $this->input->post('email_address'),
    'password' => $this->hash($this->input->post('password'))
    ), TRUE);

  if (count($user)) {
// log in user
    $data = array(
      'first_name' => $user->first_name,
      'last_name' => $user->last_name,
      'email_address' => $user->email_address,
      'id' => $user->id,
      'loggedin' => TRUE
    );
    $this->session->set_userdata($data);
    return TRUE;
  }
  return FALSE;
}

Cheers
Tim




Theme © iAndrew 2016 - Forum software by © MyBB