Authenticatication checking in CodeIgniter

Wanna protect your website from unauthorized ppl. You can use session library to check authentication in CI.

At first you have to create a model for login check:

<?php
class Login_model extends Model
{
function Login_model()
{
parent::Model();
$this->load->library(‘session’);
}

function checkAuth($uName,$pass){
$this->db->select(‘*’);
$this->db->where(‘user=’,$uName);
$this->db->where(‘pass=’,md5($pass));
$this->db->where(‘enabled=’,1);
$query = $this->db->get(‘your_users_table’);
//echo $this->db->last_query();
if($query->num_rows()>0){
$data = $query->row_array();
$sessionArray = array( ‘uid’=>$data[‘ID’],
‘role’=>$data[‘your_group’],
‘name’=>$data[‘firstname’].’ ‘.$data[‘surname’],
‘logged_in’=>TRUE
);

$this->session->set_userdata($sessionArray);
$log=array(‘user_id’=>$this->session->userdata(‘uid’),
‘action_type’=>’LOGIN’,
‘item_type’=>’USER’,
‘time’=>time());
//echo $this->db->last_query();
$this->log_message($log);
return TRUE;
}else{
return FALSE;
}
}

public function check_session()
{
if ($this->session->userdata(‘uid’) AND $this->session->userdata(‘logged_in’)==’TRUE’) {
return TRUE;
} else {
return FALSE;
}
}

public function logout(){

$this->session->unset_userdata(‘id’);
$this->session->unset_userdata(‘logged_in’);
session_destroy();
$log=array(‘user_id’=>$this->session->userdata(‘uid’),
‘action_type’=>’LOGOUT’,
‘item_type’=>’USER’,
‘time’=>time());
$this->log_message($log);
}

public function log_message($logArray){
if(isset($logArray)){
$this->db->insert(‘your_log’,$logArray);
}
}
}
?>

Now add following code to your login controller:

<?php
session_start();
error_reporting(0);
class Login extends Controller {

function Login()
{
parent::Controller();
$this->load->helper(‘url’);
$this->load->library(‘session’);
$this->load->model(‘login_model’,’login’,TRUE);
}

function index()
{
/* if the form is submitted – check whether the user is already logged in or not */
if($this->login->check_session()){
redirect(‘/main’);
}
$this->load->library(‘validation’);

$rules[‘username’] = “trim|required”;
$rules[‘password’] = “required”;
$this->validation->set_rules($rules);

$fields[‘username’] = ‘Username’;
$fields[‘password’] = ‘Password’;
$this->validation->set_fields($fields);

/* check all fields are validated correctly */
if($this->validation->run() == FALSE){
$this->load->view(‘/login_view’);
}else{
$userName = $this->input->post(‘username’);
$password = $this->input->post(‘password’);

$chkAuth = $this->login->checkAuth($userName,$password);
if($chkAuth){
redirect(‘/main’); //load cpanel file – authentication successful
}else{
redirect(‘/login/invalid’); //failed auth – return to the login form
}
}
}
}
?>

for each controller within constructor function write the following code for authentication check:

$this->load->library(‘session’);
$this->load->model(‘login_model’,’login’,TRUE);

/* check whether login or not */
if(!$this->login->check_session()){
redirect(‘/login’);
}

Now your CI project is capable to authentication handling. Best of luck.

17 Responses

  1. […] Authenticatication checking in CodeIgniter […]

  2. Thank you for the snippets – useful!

  3. Estive olhando o seu codigo
    .
    Authenticatication checking in CodeIgniter
    .
    estou com dificuldades para faze-lo rodar voce teria mais alguma dica para repassar
    .
    .
    Desde já agradeço.

    I was looking at its code
    .
    Authenticatication checking in CodeIgniter
    .
    I’m having trouble running do it you would have more to pass on some advice
    .
    .
    I would thank.

  4. Gud working……..

  5. nice tut – thank you.

  6. missing something, i need “Login_view” just for example…
    where can i get that?

    beNew..

  7. i need “Login_view” too.

  8. hello Afruj aapu ,
    আপনার CodeIgniter এর Code গুলো আমার কাজে লেগেছে। ধন্যবাদ আপু…

  9. Thanks for the code. session_destroy() in Login_model and session_start() in Login controller, why should I use them? Is it really needed in CI for session class, though that are native session PHP. If i use CI i thought that is handled by CI. So i am in confusion ! Please let me know the right way……

  10. […] Authenticatication checking in CodeIgniter […]

  11. very good.I need code.

  12. You’re not using $_SESSION anywhere.

    CI’s session class does not use native PHP sessions.

    Stop bloating your code with session_start and session_destroy.

  13. Can I just say what a reduction to search out somebody who truly knows what theyre speaking about on the internet. You positively know methods to bring a difficulty to gentle and make it important. Extra individuals need to learn this and perceive this facet of the story. I cant imagine youre no more well-liked since you positively have the gift.some tips here Fastest Weightloss

  14. Howdy! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Appreciate it!

  15. Machen Sie weiter so work.I ‘ll Rückkehr oft.

Leave a comment