Addition to Auth Library tutorial
Okay, I’ve decided to write a little addition to my previous tutorial. I finished the last tutorial, my Auth library for CI, with a note:
When you use this library, you’ll have to manually adjust the standard redirects from the library. I just set them to /admin and /admin/login but this could/will be different for you offcourse. A better way would be to add an initialize() method that gets a config array passed to set these URI’s in class vars.
I have adjusted my library a little so you can initialize it, setting redirects as you please. Even more, it now automatically uses any auth.php config files too. This was really easy and I thank xwero for helping me figure it out, over at the CodeIgniter community.
All we need to do is add 2 more class variables, an extra method and let the construct expect an array. Doing this makes our library look like.
Note: I deleted functions that remained the same, so don’t just overwrite everything!
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth {
var $CI = NULL;
var $index_redirect = '/admin';
var $login_redirect = '/admin/login';
function Auth($props = array())
{
$this->CI =& get_instance();
// Load additional libraries, helpers, etc.
$this->CI->load->library('session');
$this->CI->load->database();
$this->CI->load->helper('url');
if (count($props) > 0)
{
$this->initialize($props);
}
}
/**
* initialize class preferences
*
* @access public
* @param array
* @return void
*/
function initialize($props = array())
{
if (count($props) > 0)
{
foreach ($props as $key => $val)
{
$this->$key = $val;
}
}
}
/**
*
* This function redirects users after logging in
*
* @access public
* @return void
*/
function redirect()
{
if ($this->CI->session->userdata('redirected_from') == FALSE)
{
redirect($this->index_redirect);
} else {
redirect($this->CI->session->userdata('redirected_from'));
}
}
/**
*
* This function restricts users from certain pages.
* use restrict(TRUE) if a user can't access a page when logged in
*
* @access public
* @param boolean wether the page is viewable when logged in
* @return void
*/
function restrict($logged_out = FALSE)
{
// If the user is logged in and he's trying to access a page
// he's not allowed to see when logged in,
// redirect him to the index!
if ($logged_out && $this->logged_in())
{
redirect($this->index_redirect);
}
// If the user isn' logged in and he's trying to access a page
// he's not allowed to see when logged out,
// redirect him to the login page!
if ( ! $logged_out && ! $this->logged_in())
{
$this->CI->session->set_userdata('redirected_from', $this->CI->uri->uri_string()); // We'll use this in our redirect method.
redirect($this->login_redirect);
}
}
}
/* End of file: Auth.php */
/* Location: application/libraries/Auth.php */
How does this work? When you load a library, the Loader class automatically scans for a config file with the same name as your class. So when we add auth.php to our config folder, we can pass the index_redirect and login_redirect variables to our library when we load it.
On one of my applications, my config file looks like this:
<?php $config['index_redirect'] = '/general'; $config['login_redirect'] = '/log/in'; /* End of file: auth.php */ /* Location: application/config/auth.php */
If you don’t like using config files, you could set that array in your method and use it when loading the Auth library, or when your autoloading the library, you could use the initialize() function.







Bramme.net said
on Jul 30th, 2:08 pm |
[...] Bramme.net « Els Dufourmont Addition to Auth Library tutorial [...]
Benoit said
on Aug 31st, 2:54 pm |
Hi, thanks for the nice tutorial. It is exactly what I was looking for. Where can I find the whole auth.php class?
ps: Nice background
Jesse said
on Nov 11th, 9:56 pm |
Hey! Thanks for this great tutorial series! Very helpful to me!
Could you provide some instruction on hashing the password?
Thanks Much!
Jesse
Bram said
on Nov 11th, 10:25 pm |
I’m writing a new Auth lib (it’s going rather slow), and I’ll be writing a tut about it too. Hashing will be included!
I’ll let you know when I get there
Or you could subscribe to my RSS feed!
Andy said
on Jan 3rd, 11:05 pm |
Could you please explain what the $props array is for and how it is used in the library ?
thanks for the good tutorial
Bram said
on Jan 4th, 12:23 am |
Offcourse, but I think it’s easiest if I simply refer to the user guide: see the note under the section “Passing Parameters When Initializing Your Class”
Dan said
on Feb 9th, 5:53 pm |
Thanks for the tutorial, very helpful for me. I’m newbie in CI
dhaye said
on Apr 23rd, 5:07 am |
I have some error….
Fatal error: Call to undefined method Auth::process_login() in C:\wamp\www\admin\system\application\controllers\admin.php on line 30
and I have seen in the auth.php, there is nothing about function process_login…
can you help me…?
edziffel said
on Jun 26th, 5:39 am |
Thank you very much. Working alone with no formal background in programming, tutorials like these go a very long way for me and I’m sure for the rest of us who are trying to learn but don’t really even know the proper questions to ask. Really nice first rate work.
esolvix said
on Aug 18th, 11:19 am |
Thank you for sharing this library. I still have one issue and don’t know how I can solve it:
The session library used by CI has a general configuration, where I can set the lifetime for the session. But I want to make the “keep me logged in” function available, where the session will not be destroyed automaticaly after n seconds.
Any idea how I could implement this?
Bram said
on Aug 18th, 12:01 pm |
You would do this by adding cookies. When logging in, the log_in function should check for the checkbox, if it’s set, it should set two cookies: one with the username, and one with the password hash. the logged_in function should check for cookies if the session does not exist yet, and if the cookies are set, if they contain the correct information.