在codeigniter中管理两个角色 [英] Managing two roles in codeigniter
问题描述
我有两个角色,管理员和老师。管理员和老师帐户可以登录,但是当我登录教师帐户,它将重定向到管理页面。我不知道我的代码是否是正确的。我是一个新手使用codeigniter,所以请承担与我。
I have two roles, the admin and the teacher. The admin and teacher accounts can login however when I logged in the teacher account it will redirect to admin page. I don't know if my code is right as well. I'm a newbie at using codeigniter so please bear with me.
这里是我的控制器:
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;
}
}
推荐答案
你的成员函数你实际上并不检查角色会话数据的值,你只是检查它是否存在。您需要更改:
In your members function you are not actually checking the value of the role session data, you are just checking that it exists. You need to change :
if($this->session->userdata('is_logged_in') && $this->session->userdata('role', 'admin')){
到:
if($this->session->userdata('is_logged_in') && $this->session->userdata('role') == 'admin'){
将会看到,只有一个参数可以传递到$ this-> session-> userdata()函数中:
As you will see, there is only 1 parameter that can be passed into the $this->session->userdata() function:
/**
* Fetch a specific item from the session array
*
* @access public
* @param string
* @return string
*/
function userdata($item)
{
return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
}
这篇关于在codeigniter中管理两个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!