In this post we will learn how to create PHP login script with remember me using Cookies like email and password .If user enters again on the same login page, this data will be entered in the current registration form. This type of login function helps users to reduce user experience and stop entering logins again and again.
In my previous post, I already explained how to create a PHP login page along with session variables. However, in this post, I will show you a tutorial on how to create a PHP login page and storage functionality using an array of $_COOKIE variables. In this tutorial, I use PHP cookies for specific store details such as email and password.
Output

What are the “Remember Me” features on the PHP login page and why do we need them in our web application? Especially when the user enters our web application and assumes that he has selected the “Remember me” checkbox below the password text box. The user then enters our web application or website.
Close the browser or shut down your computer. He could not get out. He can only exit by pressing the Exit button. And if he enters again with the same browser, the data is automatically entered in the login form. So this uses the “Remember Me” mail feature in the PHP login form.
Create Login HTML Form and PHP Code
index.php
<?php session_start(); if (isset($_SESSION['id'])) { header("Location:profile.php"); } // Include database connectivity include_once('config.php'); if (isset($_POST['submit'])) { $errorMsg = ""; $email = mysqli_real_escape_string($con, $_POST['email']); $password = mysqli_real_escape_string($con, $_POST['password']); if (!empty($email) || !empty($password)) { $query = "SELECT * FROM students WHERE email = '$email'"; $result = mysqli_query($con, $query); if(mysqli_num_rows($result) == 1){ while ($row = mysqli_fetch_assoc($result)) { if (password_verify($password, $row['password'])) { $_SESSION['id'] = $row['id']; $_SESSION['fullname'] = $row['fullname']; header("Location:profile.php"); }else{ $errorMsg = "Email or Password is invalid"; } if ($row) { if (!empty($_POST['remeberme'])) { setcookie("email",$email, time() + (10*365*24*60*60)); setcookie("password",$password, time() + (10*365*24*60*60)); }else{ if (isset($_COOKIE["email"])) { setcookie("email",""); } if (isset($_COOKIE["password"])) { setcookie("password",""); } } } } }else{ $errorMsg = "No user found on this email"; } }else{ $errorMsg = "Email and Password is required"; } } ?> <!Doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Login Script with Remember Me using Cookies</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> </head> <body> <div class="container" style="margin-top:50px"> <h1 style="text-align: center;">PHP Login Script with Remember Me using Cookies</h1><br> <div class="row"> <div class="col-md-4"></div> <div class="col-md-4" style="margin-top:20px"> <?php if (isset($errorMsg)) { echo "<div class='alert alert-danger alert-dismissible'> <button type='button' class='close' data-dismiss='alert'>×</button> $errorMsg </div>"; } ?> <form action="" method="POST"> <div class="form-group"> <input type="email" class="form-control" name="email" placeholder="Email" value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>"> </div> <div class="form-group"> <input type="password" class="form-control" name="password" placeholder="Password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>"> </div> <p><input type="checkbox" name="remeberme" <?php if(isset($_COOKIE["email"])) { ?> checked <?php } ?>> Remember me</p> <input type="submit" class="btn btn-warning btn btn-block" name="submit" value="Login"> </form> </div> </div> </div> </body> </html>
Create User Profile Page after Login
profile.php
<?php session_start(); if (!isset($_SESSION['id'])) { header("Location:login.php"); } ?> <!Doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>User Profile Page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> </head> <body> <div class="container" style="margin-top:50px"> <h1 style="text-align:center;">User Profile Page</h1><br> <div class="row"> <h5>Hello, <?php echo ucwords($_SESSION['fullname']); ?> <small><a href="logout.php">Logout</a></small></h5> </div> </div> </body> </html>
Create Logout Page
logout.php
<?php include_once('config.php'); session_start(); session_destroy(); session_unset($_SESSION['id']); session_unset($_SESSION['fullname']); header("Location:login.php"); ?>
You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the form below.😊