代码之家  ›  专栏  ›  技术社区  ›  Camsoft

有人能为codeigniter 1.7.x推荐一个身份验证库吗?

  •  1
  • Camsoft  · 技术社区  · 16 年前

    我刚刚开始玩codeigniter 1.7.2,我注意到没有一个内置的用户身份验证库。

    我不需要任何花哨的东西。只需在我的后台系统中对用户进行身份验证。我不需要用户和组,或权限等。我只需要一个脚本,允许用户登录。如果用户试图访问某个页面,但未登录,则应拒绝访问。但它必须是安全的,如果它使用ci验证库将是好的。

    有人能为ci 1.7.2推荐一个库吗?

    我确实注意到stackoverflow上有一个类似的帖子,但是它已经过时了,推荐的大多数库要么不支持1.7.2,要么已经不再维护。

    最重要的是必须是安全和简单的。

    3 回复  |  直到 16 年前
        1
  •  2
  •   Tom Schlick    16 年前

    dx auth或freak auth是很好的库,几乎可以满足您的所有需要

    http://codeigniter.com/wiki/DX_Auth/

    http://codeigniter.com/wiki/FreakAuth/

        2
  •  2
  •   Stephen Curran    16 年前

    我倾向于创建自己的简单身份验证库。

    首先,这是身份验证库。它在会话中保留一个用户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');
        }
    }
    
        3
  •  0
  •   Tyler Carter    16 年前

    嗯,不是很特别的ci是一个.htaccess用户/密码系统。尤其是当你只有几个用户的时候。

    它的设置很简单,并且内置在每个apache服务器中。