Skip to main content

Login with Remember Me using PHP & MySQL

User login with remember me or login with remember password is a feature of web applications to allow user to save login details to login form. This is very user friendly feature that help to reduce user attempt to type login details again and again.

So if you’re thinking about implementing login with remember me feature then you’re here at right place. In this tutorial you will learn how to implement login with remember me using PHP and MySQL. You can also build a complete User Management System with PHP & MySQL to manage user register, login, roles etc.

We will cover this tutorial step by step with live example to implement user login with remember me feature using PHP and MySQL.

Also, read:

So let’s start implementing login with remember me using PHP and MySQL. Before we begin, take a look on files structure for this example.

  • index.php
  • welcome.php
  • logout.php

Step1: Create MySQL Database Table

As we will implement live example of login with remember me functionality, so we will create a MySQL database table members to store user login details to handle user login functionality.

CREATE TABLE `members` (
  `id` int(11) NOT NULL,
  `username` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL,
  `password` varchar(250) NOT NULL,
  `authtoken` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

We will also insert user record into members table for user login.

INSERT INTO `members` (`id`, `username`, `email`, `password`, `authtoken`) VALUES
(1, 'test', 'test@webdamn.com', '827ccb0eea8a706c4c34a16891f84e7b', '91e8cc3c6bc400c70fd17c3836e01287');

Step2: Create User Login Form

In index.php, we will create user login form with remember me checkbox to implement user login with remember me functionality.

<div class="col-md-6">                    
	<div class="panel panel-info" >
		<div class="panel-heading">
			<div class="panel-title">Sign In</div>                        
		</div> 
		<div style="padding-top:30px" class="panel-body" >
			<?php if ($errorMessage != '') { ?>
				<div id="login-alert" class="alert alert-danger col-sm-12"><?php echo $errorMessage; ?></div>                            
			<?php } ?>
			<form id="loginform" class="form-horizontal" role="form" method="POST" action="">                                    
				<div style="margin-bottom: 25px" class="input-group">
					<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
					<input type="text" class="form-control" id="loginId" name="loginId"  value="<?php if(isset($_COOKIE["loginId"])) { echo $_COOKIE["loginId"]; } ?>" placeholder="email">                                        
				</div>                                
				<div style="margin-bottom: 25px" class="input-group">
					<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
					<input type="password" class="form-control" id="loginPass" name="loginPass" value="<?php if(isset($_COOKIE["loginPass"])) { echo $_COOKIE["loginPass"]; } ?>" placeholder="password">
				</div>            
				<div class="input-group">
				  <div class="checkbox">
					<label>
					  <input  type="checkbox" id="remember" name="remember" <?php if(isset($_COOKIE["loginId"])) { ?> checked <?php } ?>> Remember me
					</label>
				  </div>
				</div>
				<div style="margin-top:10px" class="form-group">                               
					<div class="col-sm-12 controls">
					  <input type="submit" name="login" value="Login" class="btn btn-success">						  
					</div>
				</div>                                
			</form>   
		</div>                     
	</div>  
</div>

Step3: Implement User Login with Remember Me

Now we will implement user login functionality with remember me functionality. If user logged in successfully and checked remember me checkbox then we will set user login details into cookie. In this example, we have set password as it is to cookie. You can make it more secure by storing hash of password to cookie.

<?php
session_start();
include_once("inc/db_connect.php");
$errorMessage = '';
if(!empty($_POST["login"]) && $_POST["loginId"]!=''&& $_POST["loginPass"]!='') {	
	$loginId = $_POST['loginId'];
	$password = $_POST['loginPass'];
	$sqlQuery = "SELECT username FROM members WHERE email='".$loginId."' AND password='".md5($password)."'";
	$resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
	$isValidLogin = mysqli_num_rows($resultSet);	
	if($isValidLogin){
		if(!empty($_POST["remember"])) {
			setcookie ("loginId", $loginId, time()+ (10 * 365 * 24 * 60 * 60));  
			setcookie ("loginPass",	$password,	time()+ (10 * 365 * 24 * 60 * 60));
		} else {
			setcookie ("loginId",""); 
			setcookie ("loginPass","");
		}
		$userDetails = mysqli_fetch_assoc($resultSet);
		$_SESSION["user"] = $userDetails['username'];
		header("location:welcome.php"); 		
	} else {		
		$errorMessage = "Invalid login!";		 
	}
} else if(!empty($_POST["loginId"])){
	$errorMessage = "Enter Both user and password!";	
}	
?>

Step4: User Logged in Welcome Page

When the user logged in successfully then the logged in user redirected to welcome.php and display logged in user with logout link.

<div class="col-md-6">                    
<h3>Welcome - <?php echo $_SESSION["user"]; ?></h3>
<br />
<p><a href="logout.php">Logout</a></p>	                    
</div> 	

Step5: Implement User Logout

In logout.php, we will implement user logout functionality.

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

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

8 thoughts on “Login with Remember Me using PHP & MySQL

  1. Hi
    I get the following error – any ideas please?
    Warning: Cannot modify header information – headers already sent by (output started at /Library/WebServer/Documents/demo/include/header.php:1) in /Library/WebServer/Documents/demo/index.php on line 17

    1. This is sometimes due to whitespace before

      Youc an use ob_start and ob_end_clean() but make sure no text is outputted. You could then set a cookie or session equal to ob_get_contents() and then use ob_end_clean() to clear the buffer. Thanks!

  2. The only thing that I modified is change the db connection host-user-pw-dbname.
    Then put the whole folder into my website server.
    Then call the index.php in that folder within the browser.
    The interface to login is show up.
    So, I put the email address : test@webdamn.com
    and the password : 827ccb0eea8a706c4c34a16891f84e7b

    However for some reason, it gave me “invalid login !”.
    Where did I do wrong ?

    Thank you.

    1. Check you’re login with original password encrypted password. It seems you’re login with encrypted password. Thanks!

  3. Where is the db_connect.php? I made my database connect with login.php itself. So I will try to make it without that command. Which codes should I add there?

  4. What is done with authToken? You created it in the db table, but it’s not addressed anywhere else in the code that I can see. How does one generate an authToken? What is its purpose? How do you implement its use for multiple members?

    Thanks!

    1. We have not implement authToken functionality here. it for when also handle user signup to verify user. thanks!

Comments are closed.