Creating a Login Remember Me Feature in PHP Tutorial
Introduction
In this tutorial, you will learn how to Create a Login Form Remember Me Feature using PHP Language. The tutorial aims to provide the students and new programmers a reference to learn with and enhance their programming capabilities using the PHP Language. To achieve this kind of feature we will be using the HTTP Cookies. Here, snippets and sample source code are provided for a better understanding about the process.
What is HTTP Cookie?
HTTP Cookie is small blocks of data sent by the web server to the user's computer or device browsers. It is consist of name and value. In PHP, cookie is a tiny file that the web server stores on the client computer. They are often used to store data, like a username, that the website can access later to customize the user's experience when they visit the site again. Only the domain from which it was issued may read cookies.
How to use HTTP Cookie in PHP?
The syntax of setting a cookie using PHP si very simple. See the following snippet for setting an cookie in PHP.
Syntax Parameters:
- Name - (string) the name of the cookie.
- Value - (string) the value of the cookie.
- Expiration - (timestamp) the expiration timestamp of the cookie.
- Path - (string) the path in which the cookie will be available
- Domain - (string) the domain in which the cookie will be available
- Secure - (boolean) if set to
true
, the cookie will be only set if the connection is secure. - HTTP Only - (boolean) if set to
true
, the cookie will be only accessible through HTTP protocol.
How to Create a Login Form Remember Me Feature in PHP?
Creating a login form with the "Remember Me" feature can be achieved using the HTTP Cookie. Using a checkbox on the login form, a developer can let the user decide whether the browser should remember their input credential for logging in or not. If the user marked it checked, the server will store the credentials data of the user to the browser using PHP Cookie. Here's an example snippet for doing it on the server side:
- <?php
- /**
- * Saving the input data to Cookie
- */
- /**
- * Store Login Credential
- */
- }else{
- /**
- * Delete Login Credential
- */
- }
The snippet above demonstrates that the cookies will be only stored if the "Remember Me" checkbox is checked otherwise it will be removed after the login process.
Example
Here's a sample web application that demonstrates the Login Form with Remember Me Feature in PHP Language. The application is a simple login system that allows the end-user to decide if they want to store their credential in the browser so they won't need to type them if they log in again in the future.
Database Schema
Create a new Database in your MySQL Server and use the following schema for the sample user table.
Authentication Script
In your source code folder create a new PHP Script that will check if the user is already authenticated.
auth.php
Login Page Interface
Creating the Login Page interface. The authentication script is included with this script.
index.php
- <?php require_once("auth.php"); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
- <style>
- html, body{
- min-height:100%;
- width:100%;
- }
- </style>
- </head>
- <body>
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <div class="container-fluid px-5 my-3 h-75">
- <div class="col-lg-4 col-md-5 col-sm-12 mx-auto my-5 pt-5" >
- <div class="card rounded-0 shadow">
- <div class="card-header">
- </div>
- <div class="card-body">
- <div class="container-fluid">
- <form id="loginForm" action="login.php" method="POST">
- <div class="mb-3">
- <input type="text" class="form-control form-control-sm rounded-0" name="username" id="username" value="<?= isset($_COOKIE['username']) ? $_COOKIE['username'] : '' ?>" required autocomplete="off">
- </div>
- <div class="mb-3">
- <input type="password" class="form-control form-control-sm rounded-0" name="password" id="password" value="<?= isset($_COOKIE['password']) ? $_COOKIE['password'] : '' ?>" required autocomplete="off">
- </div>
- <div class="mb-3">
- <div class="form-check">
- <input class="form-check-input" type="checkbox" value="" id="rememberMe" name="rememberMe" <?= (isset($_COOKIE['username']) && isset($_COOKIE['password'])) ? "checked" : '' ?>>
- <label class="form-check-label" for="rememberMe">
- Remember Me
- </label>
- </div>
- </div>
- </form>
- </div>
- </div>
- <div class="card-footer py-2">
- <div class="text-center">
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Login Script
The snippet below is the PHP Script that processes the given login credential of the users. It stores the credential data as Cookie if the "Remember Me" checkbox is checked and the credentials are valid.
login.php
- <?php
- $conn = new mysqli("localhost", "root", "", "dummy_db");
- if(!$conn)
- $stmt = $conn->prepare("SELECT * FROM `users` where `username` = ? and `password` = ?");
- $stmt->bind_param('ss', $username, $password);
- $stmt->execute();
- $result = $stmt->get_result();
- if($result->num_rows > 0){
- $data = $result->fetch_assoc();
- foreach($data as $k => $v){
- $_SESSION['login_'.$k] = $v;
- }
- /**
- * Saving the input data to Cookie
- */
- /**
- * Store Login Credential
- */
- }else{
- /**
- * Delete Login Credential
- */
- }
- echo "<script>location.replace('./welcome.php')</script>";
- }else{
- echo "<script>alert('Username and Password does not match.'); location.replace('./')</script>";
- }
- ?>
Home Page Interface
Here's the script for the sample site's home page where the user will be redirected after successful login.
welcome.php
- <?php require_once("auth.php"); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
- <style>
- html, body{
- min-height:100%;
- width:100%;
- }
- </style>
- </head>
- <body>
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <div class="container-fluid px-5 my-3 h-75">
- <div class="col-lg-4 col-md-5 col-sm-12 mx-auto my-5 pt-5" >
- <div class="card rounded-0 shadow">
- <div class="card-header">
- </div>
- <div class="card-body">
- <div class="container-fluid">
- <dl>
- </dl>
- </div>
- </div>
- <div class="card-footer py-2">
- <div class="text-center">
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Logout Script
Here's the script for logging out.
logout.php
- <?php
Snapshot
Here's a sample snapshot of the login page of the example web application I provided using the source code above.
DEMO VIDEO
The complete source code zip file of the example source code is also provided on this site. It is free to download. The download link can be found below this article.
That's the end of this tutorial! I hope this Login Form with Remember Me Feature Tutorial in PHP will help you with what you are looking for and that you'll find it useful for your current and future PHP Projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding :)
Add new comment
- 3360 views