In my previous post we create registration form in codeigniter. In this tutorial I am going to create login form in codeigniter. I am taking the example of previous post and login using previous post data. In the download link you will get codeigniter having both registration and login code. Now let’s start.
Steps involved in Login form in Codeigniter
- Create user login form.
- Submit login form and validate form data.
- Check user email address and password.
- If both are correct then create session and send to thank you page.
Create database:
1 2 3 4 |
Create database demo; Use demo; |
Create database Table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `first_name` VARCHAR(255) NULL DEFAULT NULL, `last_name` VARCHAR(255) NULL DEFAULT NULL, `email` VARCHAR(255) NULL DEFAULT NULL, `phone` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NULL DEFAULT NULL, `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=11 ; |
Create Controller:
Login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('login_model'); } public function index() { if(isset($this->session->login) && !empty($this->session->login)){ redirect('ois', 'refresh'); } $this->load->helper('form'); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'User Name', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); if($this->form_validation->run() == false){ $this->load->view('login'); } else{ $user = $this->input->post('username'); $pass = $this->input->post('password'); $user = $this->login_model->checkLogin($user, $pass); if(is_array($user)){ $this->session->set_userdata($user); redirect('ois', 'refresh'); }else{ $data['errorMessage'] = "Wrong User or Password"; $this->load->view('login',$data); } } } public function logout(){ $this->session->sess_destroy(); redirect('login','refresh'); } function createPassword(){ $password_string = 'testing123'; $options = array( 'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM), 'cost' => 12, ); $password_hash = password_hash($password_string, PASSWORD_BCRYPT, $options); /* if (password_verify($password_string, $password_hash)) { echo 'Correct password'; } else { echo 'inCorrect password'; } */ } } |
Create Model:
User_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User_model extends CI_Model { function __construct() { parent::__construct(); } function checkDuplicate($email) { $this->db->select('email'); $this->db->from('users'); $this->db->like('email', $email); return $this->db->count_all_results(); } function insertUser($data) { if($this->db->insert('users', $data)) { return $this->db->insert_id(); } else { return false; } } function userExist($email) { $this->db->select('*'); $this->db->like('email', $email); $qry = $this->db->get('users'); $rs = $qry->result_array(); return $rs[0]; } } |
Create Views:
Login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<!doctype html> <html> <head> <title>Login form using codeigniter 3</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> </head> <style> body{ background:#d9edf7; } .custom-bottom-margin{ padding-bottom:30px; } .error-msg{ margin:5px auto; width:30%; background:#db3737; color:#ffffff; } </style> <body > <div class="container"> <div class="row"> <div class="col-md-12 text-center"> <h2>Login Form</h2> </div> </div> <div class="row"> <?php echo validation_errors(); if(isset($errorMsg)) { echo '<div class="error-msg">'; echo $errorMsg; echo '</div>'; unset($errorMsg); } ?> <form action="<?php echo base_url()."index.php/login/LoginUser"?>" method="post"> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="email">Email</label> <div class="col-sm-5"> <input type="email" name="email" class="form-control" value="<?php echo set_value('email');?>" placeholder="Enter email" id="email"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="password">Password</label> <div class="col-sm-5"> <input type="password" name="password" class="form-control" value="<?php echo set_value('password');?>" placeholder="Enter password" id="password"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right"></label> <div class="col-sm-5"> <button class="btn btn-primary" type="submit"> Submit</button> </div> </div> </form> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html> |
login-thankyou.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<!doctype html> <html> <head> <title>Thank you Registration</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> </head> <style> body{ background:#d9edf7; } .custom-bottom-margin{ padding-bottom:30px; } </style> <body > <div class="container"> <div class="row"> <div class="col-md-12 text-center"> <h2>Login</h2> </div> </div> <div class="row"> <div class="col-md-12 text-center"> You are sucessfully login <?php if(isset($this->session->email)) { echo '<a href="'.base_url().'index.php/login/logout">Logout</a>'; } ?> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html> |