Login form with Remember me in PHP using Cookie

  • Tech Area
  • January 30, 2023



In this tutorial, we discuss how to use Remember Me functionality in login form. Remember me function is used to save the user credentials like username and password at the time of login.

Remember me option allows the user to preserve their logged in status. When the user checks the Remember Me option then all the information get stored in the $_COOKIE.

Files used in this tutorial:

1- connection.php (database connection file)

2- index.php (login form with remember me functionality)

3- dashboard.php (user will redirect to this page)

4- logout.php (user session will destroy)

Below are the step by step process of how to make login form with Remember Me functionality in PHP.

Step 1: Create a Database connection

In this step, create a new file connection.php to create database connection.

connection.php

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
	echo("connection terminated");
}
?>

Step 2: Create Login form

In this step, create a new file index.php to create login form with Remember Me functionality.

This screenshot shows the UI for Remember Me with a login form.

index.php

<html>  
<head>  
    <title>Login Form</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head>
<style>
 .box
 {
  width:100%;
  max-width:600px;
  background-color:#f9f9f9;
  border:1px solid #ccc;
  border-radius:5px;
  padding:16px;
  margin:0 auto;
 }
 input.parsley-success,
 select.parsley-success,
 textarea.parsley-success {
   color: #468847;
   background-color: #DFF0D8;
   border: 1px solid #D6E9C6;
 }

 input.parsley-error,
 select.parsley-error,
 textarea.parsley-error {
   color: #B94A48;
   background-color: #F2DEDE;
   border: 1px solid #EED3D7;
 }

 .parsley-errors-list {
   margin: 2px 0 3px;
   padding: 0;
   list-style-type: none;
   font-size: 0.9em;
   line-height: 0.9em;
   opacity: 0;

   transition: all .3s ease-in;
   -o-transition: all .3s ease-in;
   -moz-transition: all .3s ease-in;
   -webkit-transition: all .3s ease-in;
 }

 .parsley-errors-list.filled {
   opacity: 1;
 }
 
 .parsley-type, .parsley-required, .parsley-equalto{
  color:#ff0000;
 }
.error
{
  color: red;
  font-weight: 700;
} 
</style>

 <body>  
    <div class="container">  
    <div class="table-responsive">  
    <h3 align="center">Login Form</h3><br/>
    <div class="box">
     <form id="validate_form" method="post" >  
       <div class="form-group">
       <label for="email">Email</label>
       <input type="text" name="email" id="email" placeholder="Enter Email" required 
       data-parsley-type="email" data-parsley-trigg
       er="keyup" class="form-control" value="<?php echo $emailid; ?>"/>
      </div>
      <div class="form-group">
       <label for="password">Password</label>
       <input type="password" name="pwd" id="pwd" placeholder="Enter Password" required  data-parsley-trigger="keyup" class="form-control" value="<?php echo $password; ?>"/>
      </div>
     <input type="checkbox" name="rememberMe" id="rememberMe"> <label for="rememberMe">Remember me</label>
      <div class="form-group">
       <input type="submit" id="login" name="login" value="LogIn" class="btn btn-success" />
       </div>
       
       <p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
     </form>
     </div>
   </div>  
  </div>
 </body>  
</html>  

Now, check the login credentials.

$email = $_REQUEST['email'];
$pwd = md5($_REQUEST['pwd']);
$select_query = mysqli_query($connection,"select * from tbl_student where email='$email' and password='$pwd'");
$res = mysqli_num_rows($select_query);

If credentials are correct, then store the session $_SESSION[‘name’] in a variable. If user check remember me checkbox then set cookie for time()+60*60 (1 Hour) otherwise set cookie for time()-10 (10 Seconds) for both variables and the user will redirect to dashboard.php. If credentials do not match, it will show error message “Please enter valid Emailid or Password”.

if($res>0)
  {
    $data = mysqli_fetch_array($select_query);
    $name = $data['name'];
    $_SESSION['name'] = $name;
    if(isset($_REQUEST['rememberMe']))
    {
      setcookie('emailid',$_REQUEST['email'],time()+60*60);//1 hour
      setcookie('password',$_REQUEST['pwd'],time()+60*60); //1 hour
    }
    else
    {
      setcookie('emailid',$_REQUEST['email'],time()-10);//10 seconds
      setcookie('password',$_REQUEST['pwd'],time()-10); //10 seconds
    }
    header('location:dashboard.php');
  }
  else
  {
    $msg = "Please enter valid Emailid or Password.";
  }

Now, check if cookies are set then cookie values assign to a variables otherwise variables value null.

if(isset($_COOKIE['emailid']) && isset($_COOKIE['password']))
{
  $emailid = $_COOKIE['emailid'];
  $password = $_COOKIE['password'];
}
else
{
  $emailid = $password ="";
}

Step 3: Create dashboard file

In this step, create a new file dashboard.php.

dashboard.php

<?php
session_start();
if(empty($_SESSION['name']))
{
	header('location:index.php');
}
if(!empty($_SESSION['name']))
{
$username = $_SESSION['name'];
}
?>
<center><h2>Welcome, <?php if(!empty($username)){ echo $username; }?> to the dashboard</h2></center>
<center><h3><a href="logout.php">Logout</a></h3></center>

Step 4: Create logout file

In this step, create a new file logout.php.

logout.php

<?php
session_start();
unset($_SESSION['name']);
header('location:index.php');
?>

Source Code

Here is the full code that we have written for index.php.

<html>  
<head>  
    <title>Login Form</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head>
<style>
 .box
 {
  width:100%;
  max-width:600px;
  background-color:#f9f9f9;
  border:1px solid #ccc;
  border-radius:5px;
  padding:16px;
  margin:0 auto;
 }
 input.parsley-success,
 select.parsley-success,
 textarea.parsley-success {
   color: #468847;
   background-color: #DFF0D8;
   border: 1px solid #D6E9C6;
 }

 input.parsley-error,
 select.parsley-error,
 textarea.parsley-error {
   color: #B94A48;
   background-color: #F2DEDE;
   border: 1px solid #EED3D7;
 }

 .parsley-errors-list {
   margin: 2px 0 3px;
   padding: 0;
   list-style-type: none;
   font-size: 0.9em;
   line-height: 0.9em;
   opacity: 0;

   transition: all .3s ease-in;
   -o-transition: all .3s ease-in;
   -moz-transition: all .3s ease-in;
   -webkit-transition: all .3s ease-in;
 }

 .parsley-errors-list.filled {
   opacity: 1;
 }
 
 .parsley-type, .parsley-required, .parsley-equalto{
  color:#ff0000;
 }
.error
{
  color: red;
  font-weight: 700;
} 
</style>
<?php
session_start();
include('connection.php');
if(isset($_COOKIE['emailid']) && isset($_COOKIE['password']))
{
  $emailid = $_COOKIE['emailid'];
  $password = $_COOKIE['password'];
}
else
{
  $emailid = $password ="";
}
if(isset($_REQUEST['login']))
{
  $email = $_REQUEST['email'];
  $pwd = md5($_REQUEST['pwd']);
  $select_query = mysqli_query($connection,"select * from tbl_student where email='$email' and password='$pwd'");
  $res = mysqli_num_rows($select_query);
  if($res>0)
  {
    $data = mysqli_fetch_array($select_query);
    $name = $data['name'];
    $_SESSION['name'] = $name;
    if(isset($_REQUEST['rememberMe']))
    {
      setcookie('emailid',$_REQUEST['email'],time()+60*60);//1 hour
      setcookie('password',$_REQUEST['pwd'],time()+60*60); //1 hour
    }
    else
    {
      setcookie('emailid',$_REQUEST['email'],time()-10);//10 seconds
      setcookie('password',$_REQUEST['pwd'],time()-10); //10 seconds
    }
    header('location:dashboard.php');
  }
  else
  {
    $msg = "Please enter valid Emailid or Password.";
  }
}
?>
 <body>  
    <div class="container">  
    <div class="table-responsive">  
    <h3 align="center">Login Form</h3><br/>
    <div class="box">
     <form id="validate_form" method="post" >  
       <div class="form-group">
       <label for="email">Email</label>
       <input type="text" name="email" id="email" placeholder="Enter Email" required 
       data-parsley-type="email" data-parsley-trigg
       er="keyup" class="form-control" value="<?php echo $emailid; ?>"/>
      </div>
      <div class="form-group">
       <label for="password">Password</label>
       <input type="password" name="pwd" id="pwd" placeholder="Enter Password" required  data-parsley-trigger="keyup" class="form-control" value="<?php echo $password; ?>"/>
      </div>
     <input type="checkbox" name="rememberMe" id="rememberMe"> <label for="rememberMe">Remember me</label>
      <div class="form-group">
       <input type="submit" id="login" name="login" value="LogIn" class="btn btn-success" />
       </div>
       
       <p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
     </form>
     </div>
   </div>  
  </div>
 </body>  
</html>  

Download Source Code


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube