In this tutorial, we will learn to active remember me checkbox  while login a page.

This concept is basically used when we want to remain login on a page and on sudden system failure or shut down and browser failure.

In such situation, we store remember me value submitted by users in session and use it in different pages of our application until users get logout.


 You can also refer our live demo or download the Script file. Extract the downloaded files, save it in your local server and run it using given path 

http://localhost/ci_remember_me/


To create a login form using remember me checkbox, it is required to have proper knowledge about CodeIgniter Session.

But if you don’t have proper knowledge on CodeIgniter Session, don’t be upset. Read our blog Session In CodeIgniterCodeIgniter Simple Login Form With Sessions and easily got the concept of CodeIgniter Sessions.

To add the functionality of remember me checkbox in CodeIgniter, we need to download a library. We will use this library in our application.

Note : – You can download the library from the given link.

First of all, extract the file and copy Extend_Session.php to application/libraries folder.

Now, rename Extend_Session.php to MY_Session.php and also edit the name of class to MY_Session.

You can also use Extend_Session.php without making any changes in the file. But for this you have to make one changes in config file.

Open  application/config/config.php and change $config[‘subclass_prefix’] = ‘MY_’; to the cofiguration given below.

$config['subclass_prefix'] = 'Extend_';

Note : Extend_  and MY_ are the prefix which must be written before session.  

Now, we are ready to use this library.

Simply load session library in your login controller and MY_session library will be autoloaded.

When “remember” flag is set by user, you can set the session as :-

$remember = $this->input->post('remember_me');
if($remember){
$this->session->set_userdata('remember_me", true);
}

 


 Controllers : user_authentication.php

In controller we first check for session data. If it is set then it will redirect user to admin page.

If session is not set then it will follow the login procedure and set remember me value provided by the user in to session.

<?php

Class User_Authentication extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form helper library
$this->load->helper('form');

// Load form validation library
$this->load->library('form_validation');

// Load session library
$this->load->library('session');
}

// Show login page
public function index() {
$this->load->view('login_form');
}

// Check for user login process
public function user_login_process() {

// Retrieve session data
$session_set_value = $this->session->all_userdata();

// Check for remember_me data in retrieved session data
if (isset($session_set_value['remember_me']) && $session_set_value['remember_me'] == "1") {
$this->load->view('admin_page');
} else {

// Check for validation
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($username == "fugo" && $password == "formget") {
$remember = $this->input->post('remember_me');
if ($remember) {

// Set remember me value in session
$this->session->set_userdata('remember_me', TRUE);
}
$sess_data = array(
'username' => $username,
'password' => $password
);
$this->session->set_userdata('logged_in', $sess_data);
$this->load->view('admin_page');
} else {
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
}

// Logout from admin page
public function logout() {

// Destroy session data
$this->session->sess_destroy();
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}

}?>

Views : login_form.php

Copy the below code in your view and save as login_form.php



<html>
<head>
<title>CodeIgniter Remember Me Checkbox</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>
<?php
if (isset($logout_message)) {
echo "<div class='message'>";
echo $logout_message;
echo "</div>";
}
?>
<?php
if (isset($message_display)) {
echo "<div class='message'>";
echo $message_display;
echo "</div>";
}
?>
<div id="main">
<div id="login">
<h2>CodeIgniter Remember Me Checkbox</h2>
<?php echo form_open('user_authentication/user_login_process'); ?>
<?php
echo "<div class='error_msg'>";
if (isset($error_message)) {
echo $error_message;
}
echo validation_errors();
echo "</div>";
?>
<label>UserName :</label>
<input type="text" name="username" id="name" placeholder="username"/>
<label>Password :</label>
<input type="password" name="password" id="password" placeholder="**********"/>
<input type="checkbox" name="remember_me"/> Remember Me
<input type="submit" value=" Login " name="submit"/>
<?php echo form_close(); ?>
</div>
<div id="note"><span><b>Note : </b></span> Please Use Following Username And Password for Login in this DEMO.
<b>username : </b>fugo<b>password : </b>formget
</div>
</div>
</body>
</html>

Views : admin_page.php

Copy the below code in your view and save as admin_page.php

<?php
$session_data = $this->session->userdata('logged_in');
?>
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="profile">
<?php
echo "Hello <b id='welcome'><i>" . $session_data['username'] . "</i> !</b>";
echo "Welcome to Admin Page";
?>
</div>
<b id="logout"><a href="logout">Logout</a></b>
</body>
</html>

CSS : style.css

Styling HTML Elements.

#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
span{
color:red;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#login{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: -8px;
}
input[type=text],input[type=password], input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
#profile{
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7;
}
#logout{
float: right;
padding: 5px;
border: dashed 1px gray;
margin-top: -126px;
margin-right: 36px;
font-size: 20px;
}
a{
text-decoration:none;
color: cornflowerblue;
}
i{
color: cornflowerblue;
}
.error_msg{
color:red;
font-size: 16px;
}
.message{
position: absolute;
font-weight: bold;
font-size: 28px;
color: #6495ED;
left: 262px;
width: 500px;
text-align: center;
margin-top: -46px;
}
#note{
clear: left;
padding-top: 20px;
margin-left: 20px;
}

Conclusion:

This was all about that how we can create remember me check box using codeigniter session. Hope you like it, keep reading our other blogs.