CodeIgniter Login Logout Tutorial with Source Code

By | June 30, 2016

Today in this tutorial we will learn Codeigniter login logout tutorial. CodeIgniter had got immensely popular recently as an open source Web application framework which uses MVC architecture to make Web Development easier. It can be also used for writing web services, especially if you looking to create Application in Android and iOS, codeigniter makes it very easier, thanks to various secured features provided by the framework, which is the reason why I have today decided to cover tutorial on login logout using codeigniter.

codeigniter login logout tutorial

It’s important to know some of the key things about MVC architecture. In MVC architecture our Model interacts with the database. Controller acts an an intermediary between View and Model. View is nothing but our HTML Webpage.

Check Tutorial on Codeigniter Image UploadĀ 

Before beginning with the tutorial it is also important to understand the directory structure in codeigniter. All our Webpages i.e Login Page will be saved in View Folder. Controller and Model files will be saved in Controller and model folders respectively. So lets firstly download latest codeigniter version from the official website of codeigniter. I am assuming you have downloaded codeigniter by now. So without further adieu lets create our first HTML page I will name itĀ Login it’s important to remember we have to save this file with .PHP extension we will be simply writing our HTML content in this file.

Download Code

Live Demo

Simple Codeigniter Login Logout Tutorial

register.php

<html>
<head>
<title>Codeigniter Login Logout Example</title>
<style>
 #Form_data
 {
 
 position: relative;
 left: -5px;
 top: 235px;
 width: 204px;
 margin: 0px auto;
 text-align: left;
 padding: 20px;
 background-color: #ffffff;
 color: #333;
 border: 1px solid #e5e5e5;
 box-shadow: rgba(200,200,200,0.7) 0 4px 10px -1px;

 }
 .buttons
 {
 border-color: #3ac162 !important;
 color: #fff;
 background-color: #5fcf80 !important;
 }
</style>
</head>

<body>
<form action ="<?php echo base_url();?>index.php/User/Save" method="POST">
<div id="Form_data">
<label><b>TechPlugg Register</b></label></br></br>
<label style="color:#555">Email</label><br/>
<input type="text" name="email" id="email"></br></br>
<label style="color:#555">Password</label><br/>
<input type="password" name="password" id="password"></br></br>
<input type="submit" value="Register" class="buttons" name="Register" >
<input type="reset" id="reset" class="buttons"> 
<p><a href="<?php echo base_url();?>index.php/User/">Login</a></p> 
</div>
</form>
</body>
</html>

login.php

<html>
<head>
<title>Codeigniter Login Logout Example</title>
<style>
 #Form_data
 {
 
 position: relative;
 left: -5px;
 top: 235px;
 width: 204px;
 margin: 0px auto;
 text-align: left;
 padding: 20px;
 background-color: #ffffff;
 color: #333;
 border: 1px solid #e5e5e5;
 box-shadow: rgba(200,200,200,0.7) 0 4px 10px -1px;

 }
 .buttons
 {
 border-color: #3ac162 !important;
 color: #fff;
 background-color: #5fcf80 !important;
 }
 #ads
 {
 position:absolute;
 top:10px;
 left:300px;
 }
</style>
</head>

<body>
<form action ="<?php echo base_url();?>index.php/User/Login" method="POST">
<div id="Form_data">
<label><b>TechPlugg Login</b></label></br></br>
<label style="color:#555">Email</label><br/>
<input type="text" name="email" id="email"></br></br>
<label style="color:#555">Password</label><br/>
<input type="password" name="password" id="password"></br></br>
<input type="submit" value="Login" class="buttons" name="login" >
<input type="reset" id="reset" class="buttons"> 
<p><a href="<?php echo base_url();?>index.php/User/register">Register</a></p> 
</div>
</body>
</html>

welcome.php

<html>
<form action ="<?php echo base_url();?>index.php/User/Logout" method="POST">
<input type="submit" value="Logout" name="logout" >
<p>Welcome to TechPlugg dashboard</p> 
</form>
</html>

User Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller 
{

 function __construct()
 {
 // Call the Model constructor
 parent::__construct();
 $this->load->database();
 $this->load->library('session');
 $this->load->helper('form');
 $this->load->library('form_validation');
 $this->load->model('User_Model');
 $this->load->helper('url');
 $this->load->library('parser');
 }

 public function index()
 {
 $this->load->view('login');
 }
 public function register()
 {
 $this->load->view('Register');
 }
 public function Save()
 {
 
 $this->form_validation->set_rules('email', 'email', 'required');
 if ($this->form_validation->run() == false)
 {
 echo 'Please enter correct email.';
 exit;
 }
 
 $this->form_validation->set_rules('password', 'password', 'required');
 if ($this->form_validation->run() == false)
 {
 
 echo 'Please enter password.';
 exit;
 }
 
 if ($this->form_validation->run() == true)
 {
 
 $email = $this->input->post('email');
 $password = md5(trim($this->input->post('password')));
 $res = $this->User_Model->Save($email,$password);
 exit;
 }
 }
 public function Login()
 {
 $this->form_validation->set_rules('email', 'email', 'required');
 if ($this->form_validation->run() == false)
 {
 echo 'Please enter correct email.';
 exit;
 }
 
 $this->form_validation->set_rules('password', 'password', 'required');
 if ($this->form_validation->run() == false)
 {
 
 echo 'Please enter password.';
 exit;
 }
 
 if ($this->form_validation->run() == true)
 {
 
 $email = $this->input->post('email');
 $password = md5(trim($this->input->post('password')));
 $res = $this->User_Model->login($email,$password);
 echo $res; 
 exit;
 }
 }
 public function dashboard()
 {
 $this->load->view('welcome');
 }
 
 public function logout()
 {
 $this->session->sess_destroy();
 redirect(base_url().'index.php/User/'); 
 }
}


?>

User Model

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class User_Model extends CI_Model 
{

 function __construct()
 {
 // Call the Model constructor
 parent::__construct();
 $this->load->database();
 $this->load->library('session');
 
 }
 
 public function signin()
 {
 $this->load->view('login');
 }
 
 public function Login($email,$password) 
 {
 
 $qry = $this->db->get_where('users',array('Email'=>$email, 'Password'=>$password));
 
 if ($qry->num_rows()>0) 
 {
 
 $row= $qry->row();
 $this->session->set_userdata($email); //setting session

 //You can further redirect users to Dashboard by using
 redirect(base_url().'index.php/User/dashboard');
 exit;
 }
 else 
 {
 echo 'Incorrect Username or Password';
 exit;
 //redirect(base_url().'index.php/User/'); redirect back to login
 }
 }
 
 public function Save($email,$password) 
 {
 
 $sql ="INSERT INTO users (Email,Password) values ('$email','$password')";
 $result = $this->db->query($sql);
 redirect(base_url().'index.php/User/'); 
 }
} 
?>

Additionally we need to make couple of changes in config.php and database.php files. In config file if you don’t use my source code then you need to change the Base URL and database details in database.php file.

Codeigniter Registration Form

Don’t forget to check php login script.

Also check URL Shortener Tutorial in PHP

Don’t forget to subscribe to get more tutorials.