CodeIgniter User/Admin Login
Let see how to create user login/Admin login page with session in codeIgnitor . CodeIgnitor based on MVC(Model View Controller), and can be also modified to use Hierarchical Model View Controller which allows developers to maintain modular grouping of Controller, Models and View arranged in a sub-directory format. I’m using here HMVC method in my example however it can be used in normal MVC method too.
First download codeIgnitor from https://codeigniter.com/download and extract zip file and copy all files and paste it in your localhost.
Once installed and configured create a database table for admin. Below is the code of my table however you can change it as per your requirement.
CREATE TABLE `admin_users` (
`id` int(11) NOT NULL,
`config_type` varchar(40) DEFAULT NULL,
`admin_username` varchar(30) DEFAULT NULL,
`admin_password` varchar(255) DEFAULT NULL,
`admin_email` varchar(60) DEFAULT NULL,
`admin_status` smallint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Next create controller file in Controller folder “admin.php” and paste below code in just created file. Please note class name of controller should start with capital letter here i have used “class Admin”. Also load model name “loginamdin” inside _construct() function to load the model of this controller.
<?php
defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);
class Admin extends Backend_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* – or –
* http://example.com/index.php/welcome/index
* – or –
* Since this controller is set as the default controller in
* config/routes.php, it’s displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->model(‘loginadmin’);
}
public function post_adlogin($data=NULL)
{
$loggedin=$this->loginadmin>adminlogin($_POST);
echo $loggedin;
if($loggedin==true){
redirect(‘/home’); //redirect to main controller
}
}
}
Now create loginadmin model in model folder and copy below code and paste in login model file.
<?php
class loginadmin extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function adminlogin($data){
//print_r($data);
$this->db->select(‘*’);
$this->db->from(‘admin_users’);
$wharray = array(‘admin_username’ => $data[‘admin_username’], ‘admin_password’ => md5($data[‘admin_password’]));
$this->db->where($wharray);
$query = $this->db->get();
$num = $query->num_rows();
if($num==1){
$adminlogin = $query->result_array();
$sessiondata = array(
‘admin_username’=>$adminlogin[0][‘admin_username’],
‘admin_password’=>$adminlogin[0][‘admin_password’],
‘logged_in’=>TRUE
);
$this->session->set_userdata($sessiondata);
return true;
}else{
$this->db->error(); // Has keys ‘code’ and ‘message’
}
}
public function adminlogout($data){
session_destroy();
}
}
Now create a login form in view folder for that create new file admin_login.php in folder and paste below code in login form file
<?php echo form_open(”,array(‘id’=>’formpages’, ‘name’=>’formpages’, ‘class’=>’form-horizontal’)); ?>
<fieldset>
<label>
<span class=”block input-icon input-icon-right”>
<input type=”text” class=”span12″ placeholder=”Username” id=”admin_username” name=”admin_username”/>
<i class=”icon-user”></i>
</span>
</label>
<label>
<span class=”block input-icon input-icon-right”>
<input type=”password” class=”span12″ placeholder=”Password” id=”admin_password” name=”admin_password”/>
<i class=”icon-lock”></i>
</span>
</label>
</fieldset>
<?php echo form_close();?>
Instead of default form validation of codeigniter i have used jquery for the same. Below is the code which can be used for valildation and form submission.
$(document).ready(function () {
$(“#adlogin”).click(function (){
dataString = $(“#formpages”).serialize();
$.ajax({
type: “POST”,
url: “admin/post_adlogin”,
data: dataString,
success: function(data){
if(data==’1′){
window.location.href = “home”;
return true;
}else{
alert(“Username and Password doesn’t match! Please try again.”);
return false;
}
}
});
return false; //stop the actual form post !important!
});
});
Leave a Reply