2017-08-11 262 views
0

我有一個管理員登錄,如果登錄好>我來到我的管理員/儀表板。但現在我嘗試在成員/儀表板下創建第二個用戶面板。CodeIgniter管理員和用戶登錄

我想我只有訪問管理員/儀表板,如果用戶數據庫表用戶> is_admin = 1。如果我沒有is_admin = 1,我收到一條錯誤消息。這是從我的代碼開始。

class Login extends CI_Controller { 

    public function index() { 
     $this->lang->load('users'); 
     $this->layout = 'ajax'; 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('email', 'lang:users_email', 'required|callback_check'); 
     $this->form_validation->set_rules('password', 'lang:users_password', 'required'); 
     if ($this->form_validation->run() == FALSE) { 
      $this->load->view('login/index'); 
     } else { 
      $user = $this->db->where('email', $this->input->post('email'))->where('password', md5($this->input->post('password')))->get('users')->row(); 
      $this->session->set_userdata(array(
       'email' => $user->email, 
       'image3' => $user->image3, 
       'user_id' => $user->user_id, 
       'username' => $user->username 
      )); 
      if($user->is_admin == 1) 
      redirect('admin/dashboard'); 
     else 
      redirect('member/dashboard'); 
     } 
    } 

    public function check() { 
     $user = $this->db->where('email', $this->input->post('email'))->where('password', md5($this->input->post('password')))->get('users')->row(); 
     if (!$user) { 
      $this->form_validation->set_message('check', lang('users_invalid_email_or_password')); 
      return FALSE; 
     } 
     else 
      return TRUE; 
    } 

} 
+0

真的沒有那麼多明確什麼是你的問題得到解決。 – JYoThI

+0

使if_admin表單不是問題,如if($ user-> is_admin == 1) 重定向('admin/dashboard');其他 重定向('member/dashboard');問題是,我也可以去管理儀表板。 –

+0

'我收到一條錯誤消息'你得到的錯誤信息是什麼? – JYoThI

回答

0

,我認爲你應該檢查查詢結果

$user = $this->db->where('email', $this->input->post('email'))->where('password', md5($this->input->post('password')))->get('users'); 
if($user->num_rows()<1){ 
    //What you want to do here 
    return false; 
} 
$user = $user->row(); 
$this->session->set_userdata(array(
    'email' => $user->email, 
    'image3' => $user->image3, 
    'user_id' => $user->user_id, 
    'username' => $user->username 
)); 
if($user->is_admin == 1) 
    redirect('admin/dashboard'); 
else 
    redirect('member/dashboard'); 

這個問題應該這樣