Ben kendi basit bir kimlik doğrulama kütüphanesi rulo eğilimindedir.
İlk olarak, bu kimlik doğrulama kütüphanedir. Bu oturumda bir kullanıcı kimliği belirteci tutar. Kimlik doğrulaması yaparken bu belirteci varlığını denetler.
application/libraries/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;
    }
}
Ben kendi tabanı denetleyicisi oluşturmak ve orada kimlik doğrulaması. Kolaylık sağlamak için, doğrulanmış ise, temel denetleyicisi yükler ve depolar geçerli kullanıcı.
application/libraries/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');
        }
    }
}
Daha sonra herhangi bir eylem ben taban denetleyicisi kimlik doğrulama işlevini çağırabilirsiniz.
class Items extends MY_Controller
{
    function Items()
    {
        parent::MY_Controller();
    }
    function create()
    {
        // Do authentication
        $this->do_auth();
        // Continue with handling request
    }
}
Ben isterseniz ben de bütün bir denetleyicisi güvenli olabilir.
class Items extends MY_Controller
{
    function Items()
    {
        parent::MY_Controller();
        // Secure entire controller
        $this->do_auth();
    }
}
Ben bir Kullanıcıların denetleyicisi giriş yerleştirin ve eylemleri çıkış. Giriş eylem olarak ben kullanıcının kimlik bilgilerini doğrulamak ve kullanıcı oturum açın.
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');
    }
}