Login and Logout with Codeigniter

Kailash Singh

Login and Logout with Codeigniter

Published Feb 26,2019 by Kailash Singh

1 Comment     4302 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>


Step 3 :- Create Login Controller in Controller Folder (Login.php) 


class Login extends CI_Controller{

	public function index(){


        // here i am creating login validation

        // if validation is correct or not
			$email = $this->input->post('email');
			$pass = $this->input->post('password');
            //check email or password is correct
			$login = $this->login_model->login_data($email, $pass);
                //Create Session
				$data = array();
				$data['id'] = $login->id;
				$data['name'] = $login->name;
				return redirect('dashboard');
				// if email or password is wrong show flash message
				$this->session->set_flashdata('fail_login', 'invalid Credentials.');
				return redirect('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).

class Login_model extends CI_Model
	public function login_data($email, $pass){
         $query = $this->db->select('*')
		 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

    class Logout extends CI_Controller
	    public function index()
		    return redirect('login');



Hope this will help our developers.

1 Comment

Ranjeet Mar 10, 2019

Its working for me.


Support Us

Subscribe My YouTube Channel

Join Our Telegram Channel & Support Eachother