Login and Logout with Codeigniter
Login and Logout with Codeigniter
Published Feb 26,2019 by Kailash Singh
1 Comment 5075 Views
In this tutorial, we are going to learn about creating a simple login form in CodeIgniter.
Before starting, let’s have a look on what we are going to learn about.
Create login page.
Setting up validation to all input field.
Check for existing users in database during signup process.
Check for username and password in database and show their information stored in database.
Create session for dashboard, store users input data in session and destroy session(logout).
Step 1 :- Create table in database.
CREATE TABLE `codeigniter`.`user` ( `id` INT(11) NOT NULL AUTO_INCREMENT , `name` VARCHAR(200) NOT NULL , `email` VARCHAR(250) NOT NULL , `password` VARCHAR(100) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
Step 2 :- Create Login Page in View Folder (login.php)
<h1 style="text-align: center;">Welcome to Login</h1>
<form action="<?php echo base_url(); ?>login" method="post">
<?php echo form_error('email',"<p class='text-danger'>","</p>");?>
<?php echo form_input(['name'=>'email','class'=>'form-control','placeholder'=>'Enter Email']);?><br>
<?php echo form_error('password',"<p class='text-danger'>","</p>");?>
<?php echo form_input(['name'=>'password','class'=>'form-control','placeholder'=>'Enter Password']);?><br>
<?php echo form_submit(['name'=>'submit','class'=>'btn btn-primary','value'=>'Login']);?><br>
</form>
Step 3 :- Create Login Controller in Controller Folder (Login.php)
<?php
class Login extends CI_Controller{
public function index(){
$this->load->helper('form');
$this->load->library('form_validation');
// here i am creating login validation
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password','Password','required');
// if validation is correct or not
if($this->form_validation->run())
{
$email = $this->input->post('email');
$pass = $this->input->post('password');
//check email or password is correct
$this->load->model('login_model');
$login = $this->login_model->login_data($email, $pass);
if($login)
{
//Create Session
$data = array();
$data['id'] = $login->id;
$data['name'] = $login->name;
$this->session->set_userdata($data);
return redirect('dashboard');
}
else{
// if email or password is wrong show flash message
$this->session->set_flashdata('fail_login', 'invalid Credentials.');
return redirect('login');
}
}
else{
$this->load->view('login');
}
}
}
?>
Step 4 :- Create Flash Message in Login Page (login.php)
<?php if($error = $this->session->flashdata('fail_login')){ ?>
<p class="text-danger"><?= $error; ?></p>
<?php } ?>
Step 5 :- Create Model in Model Folder (Login_model.php).
<?php
class Login_model extends CI_Model
{
public function login_data($email, $pass){
$query = $this->db->select('*')
->from('user')
->where(['email'=>$email,'password'=>$pass])
->get();
return $query->row();
}
}
?>
Step 6 :- Create logout in Dashboard Page in view folder (dashboard.php)
Welcome to Dashboard
<a href="<?php echo base_url(); ?>logout">Logout</a>
Step 7 :- Create Logout Controller in Controller Folder
<?php
class Logout extends CI_Controller
{
public function index()
{
$this->session->unset_userdata('id');
return redirect('login');
}
}
?>
Hope this will help our developers.
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date