Menu

Create Login Form with CodeIgniter and Bootstrap

Create Login Form with CodeIgniter and Bootstrap

We have share How to create Login Form in CodeIgniter. So here we have handled login functionality by creating simple login form in CodeIgniter and Bootstrap Framework along with MySQL Database.

Step 1: Create the Database and Table
For this tutorial, you need a MySQL database with the following table:


// Table structure for table `users`
CREATE TABLE `users` (
`user_id` int(12) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`user_name` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`status` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

// Indexes for table `users`
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `email` (`email`);

// AUTO_INCREMENT for table `users`
ALTER TABLE `users`
MODIFY `user_id` int(12) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
?>


Step 2: Configure Database access
Update the file application/config/database.php in your CodeIgniter installation with your database info:

// configure database
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root', // Your username if required.
'password' => '', // Your password if any.
'database' => 'demo_DB', // Your database name.
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
?>


Step 3: Update routes file
Add code the file application/config/routes.php in your CodeIgniter installation with you controller’s name.

$route['login'] = 'users/login';
?>


Step 4: Update autoload file
In the file application/config/autoload.php you can configure the default libraries you want to load in all your controllers. For our case, we’ll load the database and session libraries, since we want to handle user sessions, and also the URL helper for internal link generation

// load libraries
$autoload['libraries'] = array('database','session');
// load helper
$autoload['helper'] = array('url');
?>


Step 5: Create Model
Create a model file named Users_model.php inside "application/models" folder.


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of Users Model
*
* @author Team TechArise
*
* @email info@techarise.com
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Users_model extends CI_Model {
// declare private variable
private $_userID;
private $_name;
private $_userName;
private $_email;
private $_password;
private $_status;

public function setUserID($userID) {
$this->_userID = $userID;
}
public function setEmail($email) {
$this->_email = $email;
}
public function setPassword($password) {
$this->_password = $password;
}

public function getUserInfo() {
$this->db->select(array('u.user_id', 'u.name', 'u.email'));
$this->db->from('user as u');
$this->db->where('u.user_id', $this->_userID);
$query = $this->db->get();
return $query->row_array();
}
function login() {
$this -> db -> select('user_id, name, email');
$this -> db -> from('user');
$this -> db -> where('email', $this->_email);
$this -> db -> where('password', $this->_password);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
}
?>


Step 6: Create controllers
Create a controllers file named Users.php inside "application/controllers" folder.

defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends CI_Controller {

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of Users Controller
*
* @author Team TechArise
*
* @email info@techarise.com
*/

public function __construct() {
parent::__construct();
$this->load->model('Users_model', 'user');
}
// Dashboard
public function index()
{
if ($this->session->userdata('is_authenticated') == FALSE) {
redirect('users/login'); // the user is not logged in, redirect them!
} else {
$data['title'] = 'Dashboard - Tech Arise';
$data['metaDescription'] = 'Dashboard';
$data['metaKeywords'] = 'Dashboard';
$this->user->setUserID($this->session->userdata('user_id'));
$data['userInfo'] = $this->user->getUserInfo();
$this->load->view('users/dashboard', $data);
}
}
// Login
public function login()
{
$data['title'] = 'Login - Tech Arise';
$data['metaDescription'] = 'Login';
$data['metaKeywords'] = 'Login';
$this->load->view('users/login', $data);
}
// Login Action
function doLogin() {
// Check form validation
$this->load->library('form_validation');

$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required');

if($this->form_validation->run() == FALSE) {
//Field validation failed. User redirected to login page
$this->load->view('users/login');
} else {
$sessArray = array();
//Field validation succeeded. Validate against database
$email = $this->input->post('email');
$password = $this->input->post('password');

$this->user->setEmail($email);
$this->user->setPassword(MD5($password));

//query the database
$result = $this->user->login();

if($result) {
foreach($result as $row) {
$sessArray = array(
'user_id' => $row->user_id,
'name' => $row->name,
'email' => $row->email,
'is_authenticated' => TRUE,
);
$this->session->set_userdata($sessArray);
}
redirect('users');
} else {
redirect('users/login?msg=1');
}
}
}
// Logout
public function logout() {
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('name');
$this->session->unset_userdata('email');
$this->session->unset_userdata('is_authenticated');
$this->session->sess_destroy();
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
redirect('login');
}
}
?>


Step 7: Create views
Create a views file named login.php inside "application/views/users" folder.



Login Form







input->get('msg')) && $this->input->get('msg') == 1) { ?>

Please Enter Your Valid Information.





















Demo  [sociallocker] Download[/sociallocker]

Ads middle content1

Ads middle content2