Jens Segers on

Codeigniter authentication library download

Download

Many of you guys prefer to just download a complete code instead of reading my 3 piece tutorial on how to create this library. For those guys I will put a basic codigniter authentication library available for download here

What does it do

This file contains the basic functions for an authentication library. It will generate unique tokens for each user that are hard to decode because each token is generated with a user specific key. It will store this token in a cookie that is then validated to check if the user is logged in or not.

For more information about this library you can read my blog posts about this starting from here.

Installation

Download the file to your computer and extract the auth.php file. Place this file within your application's library folder: /application/libraries. Then you will want to auto load this library in the /application/config/autoload.php file.

Then adjust the code to your specific environment and needings. Be sure to adjust the query that checks if the user's credentials are correct in the authenticate function.

Usage

This is how you use this library from your login controller:

class Login extends Controller {

    function index() {
        if ($this->auth->loggedin())
            redirect('admin');
        else
            $this->load->view('admin/login');
    }

    function authenticate() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $remember = $this->input->post('remember') ? TRUE : FALSE;
       
        if ($this->auth->authenticate($username, $password, $remember))
            redirect('admin');
        else
            redirect('admin/login');
    }
}

On every other page if you want to know if a user is logged in or not use the library's loggedin() function. You can adjust the code to also store the user's id so that you can identify the current user.

Webmentions

Tweet about this blog post and you will appear below!