Add “Remember Me” Feature to Your Login Page Using PHP

Most websites have “remember me” or “stay signed in” feature in their login page. By checking that option you will be able to go to your profile page or the page after login, without logging in again. If you visit the home page, you will be redirected to your profile or relevant pages automatically.

Add Remember Me PHP

Why “Remember Me” Functionality?

  • If a user has a device which is used only by themselves, They can utilize this feature to reduce the time taken for logging in.
  • Some people always forget their login credentials, In this case you don’t have to worry when you are able to login automatically.

We are going to create a simple login functionality with “Remember Me” checkbox option. For this tutorial we have four PHP pages such as index.php, login.php, home.php and logout.php




//if either cookie or session is set, redirect to home page
if((isset($_COOKIE['user']) && $_COOKIE['user'] != '') || (isset($_SESSION['user']) && $_SESSION['user'] !='')){
<!DOCTYPE html>
 <title>Remember Me</title>
<div id="container">
 <form action="login.php" method="POST">
 <input type="text" name="username" placeholder="username">
 <input type="password" name="password" placeholder="password">
 <input type="checkbox" name="remember" value="true"> Remember Me

Index page has a simple login form with a checkbox named “remember“. This form data will be sent to login.php.

At the beginning of the page we are checking whether any cookie variable or session variable is set or not. If anyone of it is available we have to redirect users directly to home.php page.



$username = $_POST['username'];
$password = $_POST['password'];
$remember = $_POST['remember'];

$con = mysqli_connect('hostname','username','password','dbname') or Die();
$query = "SELECT id FROM members WHERE username='$username' AND password='$password'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$id = $result['id'];

$cookie_name = "user";
$cookie_value = $id;
//expiriry time. 86400 = 1 day (86400*30 = 1 month)
$expiry = time() + (86400 * 30);

if($remember == 'true'){
 //setting cookie variable
 setcookie($cookie_name, $cookie_value, $expiry);
 //if your server requires to set session path
 $_SESSION['user'] = $id;

//redirecting to home page

First we have to get the id or an equivalent field from database if the login credentials is matched. If no one is matched you can show some alert kind of thing.

Then if the checkbox is checked we have to create a cookie variable named “user” with the value of the fetched id. We can set expiry date or time for the cookie, but it is optional. If you don’t give any expiry time, the user will be logged in forever until they manually clear cookies or logout of their account.

If the checkbox is not checked we have to create a session variable with the name “user” and the value of the id.

Finally, we are redirecting the page to or any profile kind of page.



if(isset($_COOKIE['user']) && $_COOKIE['user'] != ''){
 $user = $_COOKIE['user'];
 //get user data from mysql

}else if(isset($_SESSION['user']) && $_SESSION['user'] !=''){
 $user = $_SESSION['user'];
 //get user data from mysql

Show user details based on the fetched data
echo "<a href='logout.php'>Logout</a>";

At first, we are checking either a cookie or a session is set or not, if yes we have to get the user id and fetch user data from database otherwise we have to redirect user to index page.

Also we have a logout page anchor, by visiting the page user can flush the session and cookies from the browser to make them no longer logged in.



//deleting cookie by setting expirty to past time
$res = setcookie('user', '', time() - 3600);
//destroys all session variables


There is no direct method to delete a cookie in PHP, so we are setting a past time as expiry period, so that the browser will automatically remove it.

Once everything is done, we have to redirect the page to index.php

Download and use this code and give your feedback below. Also subscribe for our feed to get notified about posts instantly.

Anand Roshan

Author: Anand Roshan

An entrepreneur, programmer and a passionate artist who loves to work independently. Get social: Twitter | Google + | Artist Page

Leave a Reply

Your email address will not be published. Required fields are marked *