1
votes

I have this function for login checker, when $email and $password wrong I want add redirect to another page, like login on a facebook page, when $email and $password incorect they direct to different page but still login page(but not the same page) and sent the message it was incorect $email or $password to the page

function basisdata_cek($password){
    $email = $this->input->post('email');
    $result = $this->login->login($email,$password);
    if($result){
        $sess_array = array();
        foreach($result as $row){
            $sess_array = $arrayName = array('kode_daftar'=>$row->kode_daftar, 'email'=>$row->email, 'password'=>$row->password);
            $this->session->set_userdata('logged_in',$sess_array);
        }
        return true;
    }else{
        $this->form_validation->set_message('basisdata_cek','invalid email or password');
        return false;
    }
}

But the problem is when I add redirect page like this

}else{
    redirect(base_url('index.php/login'),'refresh');
    $this->form_validation->set_message('basisdata_cek','invalid email or password');
    return false;
}

It's still direct page when it's incorrect email & password but the message of form_validation won't show up.

1
The information is lost on the redirect because the whole process starts over. The next page call knows nothing about the last page unless you use sessions to pass info. - DFriend

1 Answers

2
votes

It is not possible to get form_validation message if you redirect. But you can use flashdata for this.

Add this $autoload['libraries'] = array('session'); in application/config/autoload.php and Put this code in your login page

if ($this->session->flashdata('basisdata_cek')) {
    echo '<div class="alert alert-warning">';
    echo '<p>' . $this->session->flashdata('basisdata_cek') . '</p>';
    echo '</div>';
  } 

And put flash $this->session->set_flashdata instead $this->form_validation->set_message before redirect :

 }else{
    $this->session->set_flashdata('basisdata_cek', 'Invalid email or password');
    redirect(base_url('index.php/login'),'refresh');        
    return false;
  }