根据代码点火器中的用户角色重定向到管理员和用户 [英] redirect to admin and user based on user role in code igniter

查看:69
本文介绍了根据代码点火器中的用户角色重定向到管理员和用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果管理员正在登录。我希望他去admin / dashboard。否则进入用户仪表板。登录的控制器如下。在用户表中,我有一列角色,值是 1和 2。 1代表管理员,2代表用户。

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.

登录用户功能

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');
        }
    }
}


推荐答案

验证用户时,必须发送一个数组作为登录调用的响应。

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.

这篇关于根据代码点火器中的用户角色重定向到管理员和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆