Login with Remember Me using CodeIgniter and MySQL
In this tutorial, we have to share how to log in with Remember Me using CodeIgniter and MySQL. Remember me function is used to save the login details in the login form entered by the user. This is a very user-friendly feature that helps the user in reducing user attempts and stop to type login details every time again and again. This is a very simple example, you can just copy-paste, and change according to your requirement.
Before started to implement the Login with Remember Me, look files structure:
- codeigniter-login-with-remember-me
- application
- config
- constants.php
- routes.php
- controllers
- Auth.php
- models
- Auth_model.php
- views
- auth
- login.php
- index.php
- templates
- header.php
- footer.php
- auth
- config
- system
- index.php
- assets
- css
- style.css
- application
A simple step-by-step guide to help you Login with Remember Me Codeigniter.
Step 1: Create MySQL Database and Table
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 |
<?php CREATE TABLE `users` ( `id` int(11) NOT NULL COMMENT 'Primary Key', `first_name` varchar(100) NOT NULL COMMENT 'First Name', `last_name` varchar(100) NOT NULL COMMENT 'Last Name', `user_name` varchar(100) NOT NULL COMMENT 'Last Name', `email` varchar(255) NOT NULL COMMENT 'Email Address', `password` varchar(255) NOT NULL COMMENT 'Password', `address` text NOT NULL, `dob` varchar(15) NOT NULL COMMENT 'Date Of Birth', `contact_no` varchar(16) NOT NULL COMMENT 'Contact No', `url` int(255) DEFAULT NULL, `verification_code` varchar(255) NOT NULL COMMENT 'verification Code', `created_date` varchar(12) NOT NULL COMMENT 'created timestamp', `modified_date` varchar(12) NOT NULL COMMENT 'modified timestamp', `status` char(1) NOT NULL COMMENT '0=pending, 1=active, 2=delete' ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table'; INSERT INTO `users` (`id`, `first_name`, `last_name`, `user_name`, `email`, `password`, `address`, `dob`, `contact_no`, `url`, `verification_code`, `created_date`, `modified_date`, `status`) VALUES (1, 'Demo', 'Demo', 'demo', 'demo@web.com', '$2y$10$jnsHMT.l7WhIYpMxyuSt0ewQkoV/P4/79MucCyd8zxZADoIw34/nW', 'Street 3111 Massachusetts Avenue, Washington, USA', '01-02-1990', '9000000001', NULL, '1', '1559545393', '1559545393', '1'); ALTER TABLE `users` ADD PRIMARY KEY (`id`); ALTER TABLE `users` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key', AUTO_INCREMENT=2; ?> |
Step 2: Create a model file
Create a model file named
Auth_model.php
inside “application/models” folder.
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 |
<?php /* * *** * Version: V1.0.1 * * Description of Auth model * * @author TechArise Team * * @email info@techarise.com * * *** */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Auth_model extends CI_Model { // Declaration of a variables private $_userName; private $_password; public function setUserName($userName) { $this->_userName = $userName; } public function setPassword($password) { $this->_password = $password; } // login method and password verify function login() { $this->db->select('id as user_id, user_name, email, password'); $this->db->from('users'); $this->db->where('email', $this->_userName); $this->db->where('verification_code', 1); $this->db->where('status', 1); //{OR} $this->db->or_where('user_name', $this->_userName); $this->db->where('verification_code', 1); $this->db->where('status', 1); $this->db->limit(1); $query = $this->db->get(); if ($query->num_rows() == 1) { $result = $query->result(); foreach ($result as $row) { if ($this->verifyHash($this->_password, $row->password) == TRUE) { return $result; } else { return FALSE; } } } else { return FALSE; } } // password verify public function verifyHash($password, $vpassword) { if (password_verify($password, $vpassword)) { return TRUE; } else { return FALSE; } } } ?> ?> |
Step 3: Create a controller file
Create a controller file named
Auth.php
inside “application/controllers” folder.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<?php /* * *** * Version: V1.0.1 * * Description of Auth Controller * * @author TechArise Team * * @email info@techarise.com * * *** */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Auth extends CI_Controller { public function __construct() { parent::__construct(); //load model $this->load->model('Auth_model', 'auth'); $this->load->library('form_validation'); } // index method public function login() { $data = array(); $data['metaDescription'] = 'Login'; $data['metaKeywords'] = 'Login'; $data['title'] = "Login - TechArise"; $data['breadcrumbs'] = array('Login' => '#'); $this->load->view('auth/login', $data); } // index method public function profile() { $data = array(); $data['metaDescription'] = 'Profile'; $data['metaKeywords'] = 'Profile'; $data['title'] = "Profile - TechArise"; $data['breadcrumbs'] = array('Profile' => '#'); $this->load->view('auth/index', $data); } // action login method function doLogin() { // Check form validation $this->load->library('form_validation'); $this->form_validation->set_rules('user_name', 'User Name', 'trim|required'); $this->form_validation->set_rules('password', 'Password', 'trim|required'); if ($this->form_validation->run() == FALSE) { //Field validation failed. User redirected to login page $this->login(); } else { $sessArray = array(); //Field validation succeeded. Validate against database $username = $this->input->post('user_name'); $password = $this->input->post('password'); $this->auth->setUserName($username); $this->auth->setPassword($password); //query the database $result = $this->auth->login(); if (!empty($result) && count($result) > 0) { foreach ($result as $row) { $authArray = array( 'user_id' => $row->user_id, 'user_name' => $row->user_name, 'email' => $row->email ); $this->session->set_userdata('ci_session_key_generate', TRUE); $this->session->set_userdata('ci_seesion_key', $authArray); // remember me if(!empty($this->input->post("remember"))) { setcookie ("loginId", $username, time()+ (10 * 365 * 24 * 60 * 60)); setcookie ("loginPass", $password, time()+ (10 * 365 * 24 * 60 * 60)); } else { setcookie ("loginId",""); setcookie ("loginPass",""); } } redirect('auth/profile'); } else { $this->login(); } } } //logout method public function logout() { $this->session->unset_userdata('ci_seesion_key'); $this->session->unset_userdata('ci_session_key_generate'); $this->session->sess_destroy(); $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0"); $this->output->set_header("Pragma: no-cache"); redirect('auth/login'); } } ?> |
Step 4: Create a view
Create a view file named
login.php
inside “application/views/auth” folder.
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 |
<?php $this->load->view('templates/header');?> <!-- container --> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Login with Remember Me using Codeigniter and MySQL</h2> </div> <form action="<?php print site_url();?>auth/doLogin" class="remember-login-frm" id="remember-login-frm" method="post" accept-charset="utf-8" enctype="multipart/form-data"> <div class="row justify-content-center"> <div class="col-12 col-md-8 col-lg-6 pb-5"> <div class="row"><?php echo validation_errors('<div class="col-12 col-md-12 col-lg-12"><div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert">×</button>', '</div></div>'); ?></div> <!--Form with header--> <div class="card border-info rounded-0"> <div class="card-header p-0"> <div class="bg-info text-white text-center py-2"> <h3><i class="fa fa-user"></i> Login</h3> </div> </div> <div class="card-body p-3"> <!--Body--> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-user text-info"></i></div> </div> <input type="text" class="form-control" id="remail" name="user_name" placeholder="Email *" value="<?php if(isset($_COOKIE["loginId"])) { echo $_COOKIE["loginId"]; } ?>"> </div> </div> <div class="form-group"> <div class="input-group mb-2"> <div class="input-group-prepend"> <div class="input-group-text"><i class="fa fa-key text-info" aria-hidden="true"></i></div> </div> <input type="password" class="form-control" id="rpassword" name="password" placeholder="Password *" value="<?php if(isset($_COOKIE["loginPass"])) { echo $_COOKIE["loginPass"]; } ?>"> </div> </div> <div class="form-group"> <div class="checkbox"> <label> <input type="checkbox" name="remember" <?php if(isset($_COOKIE["loginId"])) { ?> checked="checked" <?php } ?>> Remember Me </label> </div> </div> <div class="text-center"> <button type="submit" id="contact-send-a" class="btn btn-info btn-block rounded-0 py-2">Login</button> </div> <span><b>Note:</b>username: <code>demo@web.com</code> password: <code>demo1234</code></span> </div> </div> </div> </div> </form> </div> </section> <?php $this->load->view('templates/footer');?> |
Step 5: Create a view
Create a view file named
index.php
inside “application/views/auth” folder.
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 |
<?php $this->load->view('templates/header');?> <style> .card { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); max-width: 300px; margin: auto; text-align: center; font-family: arial; } .title { color: grey; font-size: 18px; } .button { border: none; outline: 0; display: inline-block; padding: 8px; color: white; background-color: #3498DB; text-align: center; cursor: pointer; width: 100%; font-size: 18px; } button:hover, a:hover { opacity: 0.9; } </style> <!-- container --> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Login with Remember Me using Codeigniter and MySQL</h2> </div> <span id="success-msg"></span> <form action="<?php print site_url();?>auth/doLogin" class="remember-login-frm" id="remember-login-frm" method="post" accept-charset="utf-8" enctype="multipart/form-data"> <div class="card"> <img src="<?php print HTTP_IMAGE_PATH;?>blank-profile.png" alt="John" style="height:120px; width:100%"> <br> <h3>Team Tech</h3> <p class="title">Full Stack Web Developer</p> <?php if($this->session->userdata('ci_seesion_key')) { ?> <p><?php print $this->session->userdata('ci_seesion_key')['email']; ?></p> <?php } ?> <div style="margin: 10px 0;"> <a href="#"><i class="fab fa-facebook"></i></a> <a href="#"><i class="fab fa-linkedin"></i></a> <a href="#"><i class="fab fa-twitter"></i></a> <a href="#"><i class="fab fa-instagram"></i></a> </div> <p><a href="<?php print site_url();?>auth/logout" class="button">Logout</a></p> </div> </form> </div> </section> <?php $this->load->view('templates/footer');?> |