redirect to admin and user based on user role in code igniter

风格不统一 提交于 2020-12-06 10:15:31

问题


If the admin is logging in. I want him to go to admin/dashboard. otherwise to the users dashboard. The controller of login is follow. In the users table, I have a column of 'role' and the value are '1' and '2'. 1 stands for admin and 2 for user. and there is separate table for role.

Login User function

public function login(){
    $data['title'] = 'Login';

    //validating form
    $this->form_validation->set_rules('username', 'Username', 'required');

    $this->form_validation->set_rules('password', 'Password', 'required');

    if($this->form_validation->run() ===FALSE){
        $this->load->view('templates/header');
        $this->load->view('users/login', $data);
        $this->load->view('templates/footer');
    }else{
        //Get username
        $username = $this->input->post('username');

        //Get password in md5 
        $password= md5($this->input->post('password'));

        //Login User.... passing username and password
        $user_id = $this->user_model->login($username, $password);

        //checking userid
        if($user_id){
            //creating session if user_id is present
            $user_data=array(
                'user_id'=>$user_id,
                'username'=>$username,
                'logged_in' => true
            );

            $this->session->set_userdata($user_data);
            //set message               
            $this->session->set_flashdata('user_loggedin', 'Login successful');
            redirect('posts');                  
        }else{
            //creating session if user_id is not present
            $this->session->set_flashdata('login_failed', ' Invalid credentials');
            redirect('users/login');
        }
    }
}

回答1:


while validating the user, you have to send an array as a response to login call.

$user_info = $this->user_model->login($username, $password); // User Info should be an Array $user_info = array('user_id' => '123', 'role' => '1'); if exist and $user_info = array(); if not


if(isset($user_info['user_id']) && !empty($user_info['user_id'])) {
$user_data=array(
        'user_id'=>$user_info['user_id'],
        'username'=>$username,
        'logged_in' => true
    );

$this->session->set_userdata($user_data);
$this->session->set_flashdata('user_loggedin', 'Login successful');
if($user_info['role'] == 1){
    redirect('admin/dashboard');
} else {
    redirect('user/dashboard');
}

}

Sure this will help you.




回答2:


I don't know exactly the column name what you set for user role. Say this is user_role_id and here is my example for you.

    //checking userid
    if($user_id){
        //creating session if user_id is present
        $user_data=array(
            'user_id'=>$user_id,  // you should change this variable to $user_id['id']
            'username'=>$username,
            'logged_in' => true
        );

        $this->session->set_userdata($user_data);
        //set message               
        $this->session->set_flashdata('user_loggedin', 'Login successful');
        if($user_id['user_role_id'] == 1){
            redirect('admin/dashboard', 'refresh');
        }
        else if($user_id['user_role_id'] == 2){
            redirect('users/dashboard', 'refresh');
        }                
    }else{
        //creating session if user_id is not present
        $this->session->set_flashdata('login_failed', ' Invalid credentials');
        redirect('users/login');
    }


来源:https://stackoverflow.com/questions/44600227/redirect-to-admin-and-user-based-on-user-role-in-code-igniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!