2010-03-06 105 views
1

我剛剛開始玩CodeIgniter 1.7.2,imendialtly注意到沒有內置的用戶認證庫。任何人都可以推薦CodeIgniter 1.7.x的認證庫嗎?

我不需要任何花哨。這只是爲了驗證用戶進入我的後臺系統。我不需要用戶和組,或權限等。我只需要一個腳本來允許用戶登錄。如果用戶試圖訪問一個頁面並且他們沒有登錄,他們應該被拒絕。但它必須是安全的,如果它使用CI驗證庫,那將會很好。

任何人都可以推薦CI 1.7.2的庫嗎?

我注意到在StackOverflow上有一個類似的帖子,但它已經過時了,大多數庫建議不支持1.7.2或不再維護。

最重要的是,它必須是安全和簡單的。

回答

0

那麼,不是非常具體的CI是一個.htaccess用戶/密碼系統。特別是如果你只有幾個用戶。

設置起來很簡單,並且內置於每臺Apache服務器中。

2

我傾向於推出我自己的簡單認證庫。

首先,這是認證庫。它在會話中保留用戶ID令牌。在進行身份驗證時,會檢查是否存在此令牌。

應用程序/庫/ Auth.php

class Auth 
{ 
    var $ci; 
    var $user_id; 

    function Auth() 
    { 
     // Get CodeIgniter instance 
     $this->ci = get_instance(); 

     // Fetch token from the session 
     $this->user_id = $this->ci->session->userdata('user_id'); 
    } 

    function check() 
    { 
     return $this->user_id != null; 
    } 

    function login($user_id) 
    { 
     // Set token in the session 
     $this->ci->session->set_userdata('user_id', $user_id); 

     $this->user_id = $user_id; 
    } 

    function logout() 
    { 
     // Remove token from the session 
     $this->ci->session->unset_userdata('user_id'); 

     $this->user_id = null; 
    } 
} 

創建我自己的基本控制器和驗證那裏。爲方便起見,如果通過身份驗證,基本控制器將加載並存儲當前用戶。

應用/庫/ MY_Controller.php

class MY_Controller extends Controller 
{ 
    var $user; 

    function MY_Controller() 
    { 
     parent::Controller(); 
    } 

    function do_auth() 
    { 
     if ($this->auth->check()) 
     { 
      // Authenticated. Fetch current user 
      $this->user = $this->user_model->get_user($this->auth->user_id); 
     } 
     else 
     { 
      // Not authenticated. Redirect to login page 
      redirect('users/login'); 
     } 
    } 
} 

然後,在任何行動,我可以調用基控制器的認證功能。

class Items extends MY_Controller 
{ 
    function Items() 
    { 
     parent::MY_Controller(); 
    } 

    function create() 
    { 
     // Do authentication 
     $this->do_auth(); 

     // Continue with handling request 
    } 
} 

如果我喜歡我也可以保護整個控制器。

class Items extends MY_Controller 
{ 
    function Items() 
    { 
     parent::MY_Controller(); 

     // Secure entire controller 
     $this->do_auth(); 
    } 
} 

我將登錄和註銷操作置於用戶控制器中。在登錄操作中,我驗證用戶的憑據並登錄到用戶。

class Users extends MY_Controller 
{ 
    function Users() 
    { 
     parent::MY_Controller(); 
    } 

    function login() 
    { 
     // Verify form input 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('username', 'Username', 'required'); 
     $this->form_validation->set_rules('password', 'Password', 'required'); 

     if ($this->form_validation->run()) 
     { 
      // Fetch the user based on credentials supplied 
      $user = $this->user_model->get_user_by_credentials($this->input->post('username', true), $this->input->post('password', true)); 

      if ($user != null) 
      { 
       // Credentials verified. Log the user in. 
       $this->auth->login($user->user_id); 
       redirect(''); 
      } 
      else 
      { 
       // Login failed. Show the login page. 
       $this->load->view('users/login', array('login_failed' => true)); 
      } 
     } 
     else 
     { 
      // Yet to authenticate. Show the login page. 
      $this->load->view('users/login', array('login_failed' => false)); 
     } 
    } 

    function logout() 
    { 
     $this->auth->logout(); 
     redirect('users/login'); 
    } 
} 
+0

你爲什麼要延長東西控制器大圖書館這應該在登錄功能或庫中完成? – 2010-03-06 18:56:10

+0

我本可以這樣做。但是,然後認證庫將需要重定向到我的特定登錄頁面。我認爲這更多的是我的應用程序控制器的關注。我認爲認證庫應該只關心檢查令牌的存在。 – 2010-03-06 19:10:58

+0

噢,我通常採取的方法是記錄他們嘗試訪問的頁面,將它們重定向到登錄/註冊,成功登錄後,他們被引導回原始頁面。但是你的解決方案也可以......但請記住:) – 2010-03-06 23:56:11

相關問題