CRM Hosting


Add to Technorati Favorites

CodeIgniter authentication library

Digg this story ?

This days I was searching for an authentication system to use with CodeIgniter for a little application I want to make.
I read a little about FreakAuth, but it was too much for my needs. So I decided to look at the Derek Allard’s Bamboo Invoice System.


The authentication library he built is closer to what I want. I stripped out the parts I don’t need and here’s how it looks now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
 
class User_login 
{
	function User_login()
	{
		$this->obj =& get_instance();
	}
	function is_logged_in()
	{
		if ($this->obj->session) 
		{
			if ($this->obj->session->userdata('logged_in')) 
			{
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	} 
 
	function get_sess()
	{
		$sesiune=$this->obj->session->userdata('session_id');
		return $sesiune; 
	}
 
	function is_admin()
	{
		if ($this->obj->session) 
		{
			if (($this->obj->session->userdata('logged_in')) && ($this->obj->session->userdata('type')==1)) {
				return TRUE;
			} 
		} else {
			return FALSE;
		}
 
	}
 
	function login_routine()
	{
 
		$this->obj->load->library('encrypt');
		$password = $this->obj->input->post('password');
		$username = $this->obj->input->post('username');
 
 
 
		$sql = "select * from users where username = ? and password=password(?)";
		$login_result = FALSE;
		$res=$this->obj->db->query($sql,array($username,$password));
		if ($res->num_rows()>0) 
		{
			$row=$res->row();
			$login_result = TRUE;
			$id=$row->id;
			$type=$row->type;
 
 
			$credentials = array('user_id' => $id, 'logged_in' => $login_result,'type'=>$type,'username'=>$username);
			$this->obj->session->set_userdata($credentials);
			redirect('','location');
		}	
 
 
		else {
			echo $login_result;
			redirect('login/login_fail','location');
			exit;
		}
	}
}
?>

It basically reads the username and passwords, verify them against the users database, checks if the user has administrator rights and register some session variables. It’s more than simple, but this is all I need for now.
Hope it will help you


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button
Comments are DoFollow, so you may consider writing a small note :)

5 Responses to “CodeIgniter authentication library”

  1. Awesome! I was about to throw my hands up and write my own because I needed something extremely basic, and all of the available libraries were way too bloated for this current site I’m working on. Many thanks. :)

  2. I’m glad you find it useful :)

  3. useful, thanks.

  4. Could you tell me that where should I place this kind of file?

  5. In www_root/Your_App/system/application/libraries/user_login.php
    I use to autoload it (www_root/Your_App/system/application/config/autoload.php):
    $autoload['libraries'] = array(‘User_login’);

Leave a Reply



BRDTracker