Welcome Guest, Not a member yet? Register   Sign In
TUTORIAL : simple side wide login
#1

[eluser]xwero[/eluser]
I'm back with a tutorial on how to use hooks in a creative manner. This time i'm going to demonstrate a simple authentication with side wide login form.

The common way to do this is to extend the controller and place the intercepting code there but with hooks you don't have to extend the controllers.

Code:
function login_form()
{
   $CI =& get_instance();
   $CI->load->vars(array('login',$CI->load->view('login_form'));
}

function authenticate()
{
   if(isset($_POST['login_button']))
   {
      $CI =& get_instance();
      $CI->load->model('auth_model','',TRUE);
      if($CI->auth_model->check($_POST['username'],$_POST['password']))
      {
        $CI->session->set_userdata('id',$CI->auth_model->get_id($_POST['username'],$_POST['password']));
      }
   }
}

function check_authentication()
{
   $CI =& get_instance();
   if($CI->session->userdata('id'))
   {
       $CI->load->model('users_model','',TRUE);
       $CI->load->vars(array('login',$CI->load->view('logged_in',$CI->users_model->logged_in($CI->session->userdata('id')));
   }
}
This is the content of the hooks/auth.php file.

I'm not putting the functions into a class because you will never write the functions yourself in controller code. Once namespaces are available i would add them to a namespace to minimize the risk of having a function with the same name.

The config/hooks.php looks like this
Code:
$hook['post_controller_constructor'][] = array(
                                'function' => 'login_form',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller_constructor'][] = array(
                                'function' => 'authenticate',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller_constructor'][] = array(
                                'function' => 'check_authentication',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

The placing of the hook functions is important. The first two hooks can be switched but the third has to be last.

And that is it. Now you have a reusable, basic authentication solution.


Messages In This Thread
TUTORIAL : simple side wide login - by El Forum - 01-05-2009, 04:07 AM
TUTORIAL : simple side wide login - by El Forum - 02-02-2009, 05:09 PM
TUTORIAL : simple side wide login - by El Forum - 02-03-2009, 05:18 AM
TUTORIAL : simple side wide login - by El Forum - 02-03-2009, 05:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB