Overview
After Signup and login form in php we will add remember me functionality in login form and also how set user and password cookies for 30 days and populate this username and password for login form plus tick remember me checkbox ,
in this tutorial we will also see if user want to discard or clear his or her remember me username and password we will do it step by step for demonstration.
login.php snippet code :
<form name="loginform" action="login_nextpage.php" method="get"> <input type="text" name="username" placeholder="enter username" value="<?php if(isset($_COOKIE['username'])) echo $_COOKIE['username']; ?>" required> <input type="password" id="passwordID" name="password" placeholder="enter password" value="<?php if(isset($_COOKIE['password'])) echo $_COOKIE['password']; ?>" required> <div class="checkbox"> <input name="remember" id="remember" type="checkbox" <?php if(isset($_COOKIE['username'])){echo "checked='checked'"; } ?> value="1"> <label for="remember"> Remember Me </label> </div> <input type="submit" value="Login"> </form>
login_nextpage.php snippet code :
<?php $usernameVal=$_REQUEST["username"]; //$passwordVAl=$_REQUEST["password"]; $servername = "localhost"; $username = "root"; $password = ""; $dbname = "userstest_db"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } else { $escapedPW = mysqli_real_escape_string($conn,$_REQUEST['password']); //save this user and pass as cookie if remeber checked start if (isset($_REQUEST['remember'])) $escapedRemember = mysqli_real_escape_string($conn,$_REQUEST['remember']); $cookie_time = 60 * 60 * 24 * 30; // 30 days $cookie_time_Onset=$cookie_time+ time(); if (isset($escapedRemember)) { /* * Set Cookie from here for one hour * */ setcookie("username", $usernameVal, $cookie_time_Onset); setcookie("password", $escapedPW, $cookie_time_Onset); } else { $cookie_time_fromOffset=time() -$cookie_time; setcookie("username", '',$cookie_time_fromOffset ); setcookie("password", '', $cookie_time_fromOffset); } //save this user and pass as cookie if remember checked end //now check user and pass verification $query = "select * from user where username = '$usernameVal';"; $resultSet = mysqli_query($conn,$query); if(@mysqli_num_rows($resultSet) > 0){ //check noraml user salt and pass //echo "noraml"; $saltQuery = "select salt from user where username = '$usernameVal';"; $result = mysqli_query($conn,$saltQuery); $row = mysqli_fetch_assoc($result); $salt = $row['salt']; $saltedPW = $escapedPW . $salt; $hashedPW = hash('sha256', $saltedPW); $query = "select * from user where username = '$usernameVal' and password = '$hashedPW' "; $resultSet = mysqli_query($conn,$query); if(@mysqli_num_rows($resultSet) > 0){ $row = mysqli_fetch_assoc($resultSet); echo "your username and password is corrent"; session_start(); $_SESSION["user_id"]=$row["user_id"]; $_SESSION["user_name"]=$row["username"]; header("location:index.php"); } else { echo "your username or password is incorrect"; } } } ?>
Summary
how login form remember me functionality works in php and how set cookies for 30 days and populate this username and password for login form.