2017-06-17 86 views
1

如果管理員正在登錄,我希望他去管理員/儀表板。否則傳遞給用戶儀表板。登錄的控制器是按照。在用戶表中,我有一列'角色',值爲'1'和'2'。 1代表管理員,2代表用戶。並有獨立的角色表。根據代碼點火器中的用戶角色重定向到管理員和用戶

用戶登錄功能

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

讓我看看你的登錄()方法。 – molagbal

+0

其中是user_role字段?你沒有把它放在會話中嗎? –

+0

你可能會發現這有助於:[點擊這裏](https://stackoverflow.com/questions/41529588/what-is-the-best-practice-for-role-based-login-system-in-codeigniter) –

回答

0

我不知道到底是什麼,你的用戶角色設定的列名。說這是user_role_id這是我的例子。

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

我有沒有像你說的........不能使用stdClass類型的對象作爲數組..........如果($ user_id ['user_role_id'] == 1){ 重定向('admin/dashboard','refresh'); }在此部分 –

+0

請顯示您的模型。 – kazinayem2011

+0

//函數登錄用戶憑證 \t \t公共功能登錄($用戶名,密碼$){的校驗 \t \t \t //驗證憑據的用戶名和密碼 \t \t \t $「這 - > DB->其中(用戶名',$用戶名); \t \t \t $ this-> db-> where('password',$ password); \t \t \t //從用戶表中獲取結果 \t \t \t $結果= $這個 - > DB-> GET( '用戶'); \t \t \t \t \t \t //檢查是否用戶是存在於數據庫中 \t \t \t如果($ result-> NUM_ROWS()== 1){ \t \t \t \t返回$ result->行(0 ) - >用戶ID; \t \t \t} else { \t \t \t \t return false; \t \t \t} \t \t} –

0

在驗證用戶的同時,您必須發送一個數組作爲登錄調用的響應。

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

} 

當然這會幫助你。

相關問題