How To Create Login With Remember Me Function In PHP


login with remember me php

Remember me function is used to save the username and password in login form  entered by the user using PHP. In this tutorial we are using COOKIES for saving preserving username and user password in the login from.
Step 1 :  Create a simple HTML form

<div align="center">
<form action="" method="post" id="login">
<div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>
<h4> PHP login with remember me function</h4>
<div class="field-group">
<div><label for="login">Username</label></div>
<div>
<input name="username" type="text" value="<?php if(isset($_COOKIE["user_login"])) { echo $_COOKIE["user_login"]; } ?>" class="input-field">
</div>
<div class="field-group">
        <div><label for="password">Password</label></div>
        <div><input name="password" type="password" value="<?php if(isset($_COOKIE["userpassword"])) { echo $_COOKIE["userpassword"]; } ?>" class="input-field">
    </div>
    <div class="field-group">
        <div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["user_login"])) { ?> checked <?php } ?> />
        <label for="remember-me">Remember me</label>
    </div>
    <div class="field-group">
        <div><input type="submit" name="login" value="Login" class="form-submit-button"></span></div>
    </div>
</form>
</div>

Step 2 : Create a SQL table tbluser

CREATE TABLE `tbluser` (
  `id` int(11) NOT NULL,
  `userName` varchar(200) DEFAULT NULL,
  `userPassword` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbluser`
  ADD PRIMARY KEY (`id`);

Step 3 : Create database configuration file config.php

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'test'); // your database name
$conn = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Step 4 : PHP Cookies to remember username and password. Username and password will be stored $_COOKIE array.

<?php
session_start(); // starting a session
include('config.php'); // Database configuration file
if(isset($_POST["login"]))
{
// Getting Post Vlaues
$username=$_POST['username'];
$password=md5($_POST['password']); // MD5 password encryption
$sql = "Select * from tbluser where userName="$username" and userPassword ='$password'";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result);
    if($row) {
            $_SESSION["userid"]= $row["id"];
    // if remember me clicked . Values will be stored in $_COOKIE  array
            if(!empty($_POST["remember"])) {
//COOKIES for username
setcookie ("user_login",$_POST["username"],time()+ (10 * 365 * 24 * 60 * 60));
//COOKIES for password
setcookie ("userpassword",$_POST["password"],time()+ (10 * 365 * 24 * 60 * 60));
} else {
if(isset($_COOKIE["user_login"])) {
setcookie ("user_login","");
if(isset($_COOKIE["userpassword"])) {
setcookie ("userpassword","");
                }
            }
    header('location:welcome.php');
    } else {
        $message = "Invalid Login";
    }
}
?>

Happy Coding!

Download Here



Sharing is caring!

Facebook Comments

Leave a Reply