PHP- Codeigniter根据用户角色重定向到管理员和用户

时间:2018-01-23 15:50:19

标签: php codeigniter login

如果管理员正在登录,我希望他们转到管理页面,否则转到约会页面。在users表中,我有一列'id_roles',值为'2'和'3'。 2代表admin,3代表用户,并且有角色的单独表。

这是我的控制器

public function index()
{
    // get form input
    $email = $this->input->post("email");
    $password = $this->input->post("password");

    // form validation
    $this->form_validation->set_rules("email", "Email-ID", "trim|required");
    $this->form_validation->set_rules("password", "Password", "trim|required");

    if ($this->form_validation->run() == FALSE)
    {
        // validation fail
        $this->load->view('login_view');
    }
    else
    {
        // check for user credentials
        $uresult = $this->model_user->get_user($email, $password);
        //edit
        if (count($uresult) > 0 )
        {
            // set session

            $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->first_name,'ulname' => $uresult[0]->last_name,'uemail' => $uresult[0]->email, 'uphone' => $uresult[0]->phone_number, 'uaddress' => $uresult[0]->address, 'ucity' => $uresult[0]->city,  'uin' => $uresult[0]->injury, 'uid' => $uresult[0]->id);
            $this->session->set_userdata($sess_data);
            //edit

        if($uresult->['id_roles'] == 3)
            redirect('appoinments/index');
            else 
            redirect('admin/index');

        }

        else
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
            redirect('login/index');
        }
    }
}

这是我的模特

    function get_user($email, $pwd)
{
    $this->db->where('email', $email);
    $this->db->where('password', md5($pwd));
    $query = $this->db->get('ea_users');
    return $query->result();
}

1 个答案:

答案 0 :(得分:0)

在你的&#34; ea_users&#34; table创建另一列&#34; usertype&#34;。如果您将根据usertype检索数据,则可以重定向(即分别为用户和管理员提供用户配置文件和管理面板)