我有兩個角色,管理員和老師。管理員和教師帳戶可以登錄,但是當我登錄教師帳戶時,它將重定向到管理頁面。我不知道我的代碼是否正確。我是使用codeigniter的新手,所以請耐心等待。在codeigniter中管理兩個角色
因此,這裏是我的控制器:
public function login_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('idnum', 'ID Number', 'required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
if ($this->form_validation->run()){
//$this->load->model('model_users');
$this->load->model('model_role');
$data = array(
'login_id' => $this->input->post('idnum'),
'is_logged_in' => 1,
'role' => $this->model_role->scalar('user_account','role')
);
$this->session->set_userdata($data);
redirect('site/members');
} else {
$data['title'] = "Outcome-based Education";
$this->load->view("index/header", $data);
$this->load->view("index/view_home");
$this->load->view("index/footer");
}
}
public function members(){
if($this->session->userdata('is_logged_in') && $this->session->userdata('role', 'admin')){
$this->load->view('login/admin');
} elseif($this->session->userdata('is_logged_in') && $this->session->userdata('role', 'teacher')){
$this->load->view('login/members');
}
else{
redirect('site/restricted');
}
}
這是我的模型:
public function can_log_in(){
$this->db->where('login_id', $this->input->post('idnum'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user_account');
if($query->num_rows() == 1){
return true;
} else{
return false;
}
}
那麼,你的問題是什麼? :) – 2014-11-24 09:10:40