Simple codeigniter login system

Today we are going to show you how to create a codeigniter login system in simple way. Here we are explaining step by step how can we create simple effective codeigniter login system. This codeigniter login system is tested and working fine in case if you getting any kind of errors and not understanding how to implement feel free and comment as soon as possible we will back to you.

We learned about codeigniter registration system in previous article of codeigniter.

Step 1. Create view name of “login.php” inside (application/views) folder

/*
* Author: Mukesh Jakhar
* Description: Create login view name of "login.php" inside (application/views) folder
*/
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Simple codeigniter login system</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Simple codeigniter login system" />
<meta name="keywords" content="Simple codeigniter login system" />
<meta name="author" content="Mukesh Jakhar" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="common_section">
	<div class="container">
  		<div class="login">
  			<form method="post" action="<?php echo site_url('Dashboard/login'); ?>">
  				<h2>Admin Login</h2>
				<div class="form-group">
  					<input type="email" class="form-control" placeholder="Enter your email" name="email" value="<?php echo set_value('email'); ?>" />
				</div>
				<div class="form-group">
					<input type="password" class="form-control" placeholder="Enter your password" name="password" />
				</div>
				<div class="form-group">
					<input type="submit" class="form-control" name="login" value="LOGIN" /><
				</div>
				<div class="error">
					<?php echo validation_errors(); ?>
  					<?php if(isset($error)){echo '<p>'.$error.'</p>';} ?>
  				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>

Step 2. Create Controller name of “Dashboard.php” inside (application/controllers) folder

Here a controller with name of “Dashboard”. This controller handles all the user login related works. The used functions and their functionality are given below.

__construct() – Form validation library, Session library and user model is loaded.
index() – Displays login form
login() – Check form valid, display error, load model function “validateUser()” and return result.
logout() – Logouts users from their account.

CodeIgniter built-in form validation library is used in login() to ensure that the user submits all the required fields.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* Author: Mukesh Jakhar
* Description: Login Controller class with name of "Dashboard.php"
*/
class Dashboard extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->model('Login_model');
	}
	
	public function index()
	{
		if(isset($_SESSION['user']))
		{
			$this->load->view('admin');
		}
		else
		{
			$this->load->view('login');
		}
	}
	
	function login()
	{
		$this->load->helper(array('form', 'url'));
		
		$this->form_validation->set_rules('email', 'email ', 'required|valid_email');
		$this->form_validation->set_rules('password', 'password', 'required');
		
		$user = $this->input->post('email'); 
		$pass = $this->input->post('password'); 
		if ($this->form_validation->run() != FALSE)
		{
			$res = $this->Login_model->validateUser($user, $pass);
			if($res['success']==1)
			{
				$this->session->set_userdata(array('user'=>$user)); 
				$this->load->view('admin');
			}
			else
			{
				unset($_SESSION['user']);
				$data['error'] = 'Username or password is invalid';
				$this->load->view('login', $data);
			}
		}
		else
		{
			$this->load->view('login');
		}
	}
	
	function logout()
	{
		unset($_SESSION['user']);
		$this->load->view('login');
	}
}

Step 3. Create model name of “Login_model.php” inside (application/models) folder

Here we are create a model “Login_model.php”. The model is used to check whether user entered data is exist or not. Here i am create a function inside model “validateUser()” using this function performing action.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Author: Mukesh Jakhar
* Description: Login model class
*/
class Login_model extends CI_Model{
	function __construct(){
		$this->load->database();
		parent::__construct();
	}
	
	public function validateUser($username, $password){
	
		$query = $this->db->get_where('users', array('email' => $username, 'password' => $password));
		
		if($query->num_rows() == 1)
		{
			// If there is a user, then create session data
			$row = $query->row();
			return $data = array('success' => "1", 'userid' => $row->id, 'email' => $row->email);
		}
		else
		{
			return $data = array('success' => "0");
		}
	}
}
?>

Step 4. Create Database and table

This is final step of codeigniter login system, Create a table into your database with name of the table should be “users” and required fields are “id”, “email” and “password”. if once you create “users” table then insert any data into table after that you can login.

CREATE TABLE `users` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
	`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
	PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Article Name: Simple codeigniter login system
Publish Date: 07-08-2018
Google Tags: codeigniter login system with roles, codeigniter admin login page, how to create codeigniter login system, codeigniter 3 login, login form in codeigniter with validation, codeigniter bootstrap login, user login form in codeigniter, how to create simple codeigniter login system, codeigniter login with session, codeigniter login and registration with session

Leave a Reply

Related Posts: You may like

Codeigniter pagination with search query string

Minify HTML in CodeIgniter using Hooks

Redirect http to https using htaccess

How to execute mysql query inside codeigniter helper

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.