phpcodeigniterif-statementreturn-value

Why does this CodeIgniter controller method always redirect after calling the model method?


I'm building a log in for my website. I have this if statement:

$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.

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);   
    }
}

Solution

  • You're not returning anything from your login() function. You either intended to return a TRUE or FALSE value depending on whether the login attempt succeeded (most likely), or you want to test against the session user data.