2010-03-06 56 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

相关问题