PHP Login & Remember me using Cookies
Today, we are going to how to create login with Remember me using Cookies in php(PHP Login & Remember me using Cookies).
We give you a simple example of PHP Login & Remember me using Cookies. so you can see our example maybe you like this php example.
PHP Login Form(index.php)
In this step, we will create a simple php login form and check cookie is set or not, if the cookie is set then we will display data from cookies. so in this example, we will use the PHP cookie for remember me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>PHP Login & Remember me using Cookies - XpertPhp</title> </head> <body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="/">Login</a> </div> </nav> <div class="container"> <div class="row"> <div class="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white from-wrapper"> <div class="container"> <h3>Login</h3> <hr/> <?php if (isset($_REQUEST['success'])): ?> <div class="alert alert-success" role="alert"> <?php echo $_REQUEST['success']; ?> </div> <?php endif; ?> <form name="login" method="post" action="login_process.php"> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" autocomplete="off" name="email" id="email" value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>" required autocomplete="off" /> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" autocomplete="off" name="password" id="password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" required /> </div> <div class="form-group form-check"> <label class="form-check-label"> <input class="form-check-input" name="chkRememberMe" type="checkbox" <?php if(isset($_COOKIE["remember_me"])) { echo 'checked'; } ?> /> Remember me </label> </div> <?php if (isset($_REQUEST['error'])): ?> <div class="row"> <div class="col-12"> <div class="alert alert-danger" role="alert"> <?php echo $_REQUEST['error']; ?> </div> </div> </div> <?php endif; ?> <div class="row"> <div class="col-12 col-sm-4"> <button type="submit" name="btnLogin" class="btn btn-primary">Login</button> </div> </div> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html> |
PHP Login Process code(login_process.php)
In this step, when the user submits this php login form we will check whether these fields are empty or not. if that fields are empty then we will redirect the login page with error message.
if not empty then we do further process. like as if checked remember me checkbox then we set the cookie otherwise unset the cookie. If you like this example then you can share this article with your friends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php if(isset($_POST['btnLogin'])) { $email = $_POST['email']; $password = $_POST['password']; if(!empty($email) && !empty($password)){ if(isset($_POST['chkRememberMe']) && $_POST['chkRememberMe'] == "on"){ $remember_me = $_POST['chkRememberMe']; setcookie ("email",$email,time()+ 3600); setcookie ("password",$password,time()+ 3600); setcookie ("remember_me",$remember_me,time()+ 3600); $msg="Cookies Set Successfully"; header ("Location: index.php?success=".$msg); exit; }else{ setcookie ("email",""); setcookie ("password",""); setcookie ("remember_me",""); $msg="Cookies Unset Successfully"; header ("Location: index.php?success=".$msg); exit; } }else{ $msg="All fields are mandatory"; header ("Location: index.php?error=".$msg); exit; } } ?> |