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.
Filed under: CodeIgniter | Tagged: Authentication check, CodeIgniter, PHP, security, Web Development





nice code
Thank you for the snippets – useful!
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.
Gud working……..
Good madam
nice tut – thank you.
missing something, i need “Login_view” just for example…
where can i get that?
beNew..