Login Form With Remember Me in Php

0

Hey Guys, hope you all fine, recently I’ve made a tutorial Login Form with Remember Me in Php and MySQLi. In this tutorial you will learn login form using session and cookie with remember me in PHP, I made different tutorials which are related in Login Form in Php, but still, I don’t work on a particular topic like How to use Session and Cookies to make a login form with Remember features.

Login Form With Remember Me in Php

So, I’m going to share with you step by step How to make a Login Form in Php, and also you will learn How to use Session and Cookies in the Php Application. I’ve made a video tutorial on this topic, you will watch the complete tutorial, and also you will get the source code of the project.

Login Form With Remember Me in Php

Basically, the User Registration System in Php play important role in the applications. So, you need to understand that How you can manage the Login System in Php, there are a bunch of developers, they are sharing personal knowledge on different platforms. I’m also sharing knowledge like this one, but the thing is different developers have different ideas or different usage in Programming.

I usually struggle to share with you any tutorial with practically and also simple way to use Programming to build an application. So, Let’s get start to understand How to Make Login Form with Remember Me in Php.

PHP Login Page with Session and Cookies Example

I’ve made the tutorial that help you to understand How to make Php Login Page with Session and Cookies, you need the complete tutorial, once you watch it. Then hope can understand step by step usage of Session and also cookies to make a login form in Php.

I hope you like the tutorial when you will work on your personal computer to make a login form with Remember in Php. I hope you will do that very well, If you want to know How I write the Php codes inside the project, I’m going to share with you each code that I have written inside the tutorial.

How to Use Session in Php For Login Form

Let’s talk about How to Use Session in Php Form Login Form, First of All, you need to design the login using HTML/CSS or Bootstrap. I’ve designed the Login Form using Bootstrap, you can use the same thing, but you need to get the Bootstrap CSS File and include that on your project. Once you do that, then you will able to see the same thing which is available on my video tutorial.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Login Form in Php with Remember Me</title>
</head>
<body class="bg-dark">

        <div class="container">
            <div class="row">
                <div class="col-lg-6 m-auto">
                    <div class="card mt-5">
                        <form  method="POST">
                        <div class="card-header bg-primary mt-5 mb-3">
                            <h4 class="text-white text-center">Login Form in Php With Remember Me</h4>
                        </div>
                        <div class="card-body">
                            <input type="text" name="username" class="form-control mb-3" placeholder="User Name or Email">
                            <input type="password" name="password" class="form-control mb-3" placeholder="Password">
                            <lable><input type="checkbox" name="rem" class="mb-3"> Remember Me </lable>
                        </div>
                        <div class="card-footer">
                            <button type="submit" class="btn btn-success" name="btn_login">Login</button>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

</body>
</html>

Once you designed the login form, then you need to make the connection between Php and MySQLi. You can check out the connection code below.

<?php

$con = mysqli_connect("localhost","root","","remember");
    if(!$con)
{
    die(mysqli_errno($con));
}
?>

Once you do that, then you need to make a functions.php file, inside the file we will use functions that help us to manage the application code.

session_start();
require_once 'connection.php';
 
   function set_message($msg)
{
    if(!empty($msg))
    {
        $_SESSION['MSG']=$msg;
    }
    else
        {
            $msg="";
        }
}


    function display_message()
    {
        if(isset($_SESSION['MSG']))
        {
            echo $_SESSION['MSG'];
            unset($_SESSION['MSG']);
        }

    }

I’ve shared functions.php inside the file have two functions that help you to set and also display the session message anywhere you want. I’ve used the session messages inside the project, you will see the usage of session messages.

 function user_data()
    {
        global $con;
        if(isset($_SERVER['REQUEST_METHOD'])=='POST' && isset($_POST['btn_login']))
        {
            $username = mysqli_real_escape_string($con,$_POST['username']);
            $password = mysqli_real_escape_string($con,$_POST['password']);
            $check = mysqli_real_escape_string($con,$_POST['rem']);


            if(empty($username) || empty($password))
            {
                $error_msg = '<div class="alert alert-danger">Please Fill in the Blanks</div>';
                set_message($error_msg );
            }

Here is another function namely user_data, you need to make the same function, and also you can see inside the if the body has set_message function Its session message. The Set_Message function used to set Session Message. Once you made the user_data and also set_session function then you need to copy the user_data function and paste it with the index.php file.

    require_once 'includes/functions.php';
    user_data();

once you have done that, then you can use anything inside the functions which are available functions.php file. Now Let’s talk about them to display the session message inside the index.php file. That’s very simple where you want to display the function, I want to display a message inside the index.php file, so, I just copy the function namely display_message() after the card header tag.

<div class="card-header bg-primary mt-5 mb-3">
                            <h4 class="text-white text-center">Login Form in Php With Remember Me</h4>
                        </div>
                            <?php

                                if(isset($_SESSION['MSG']))
                                {
                                    display_message();
                                }

                            ?>

Once you’ve done that, then you will able to see error message inside the index.php file. Let’s continue to user_data function another codes, I’ve written inside the user_data function empty function and you’ve seen the if body, after that you need to use else body.

 else
                {
                    $query = "select * from login_user where username='$username'";
                    $result = mysqli_query($con,$query);

then you need to use again if statement and else statements


                    if(mysqli_num_rows($result))
                    {
                       while($row = mysqli_fetch_assoc($result))
                       {
                           $username_db=$row['username'];
                           $password_db = $row['password'];

                           if(md5($password)==$password_db)
                           {

                               if($check==true)
                               {
                                   setcookie('USER_NAME',$username_db, time() + 86400 * 30);
                                   setcookie('PASSWORD',$password_db, time() + 86400 * 30);
                               }

                               $_SESSION['username']=$username_db;
                               $_SESSION['password']=$password_db;

                               header("Location:dashboard.php");
                           }
                           else
                           {
                               $error_msg = '<div class="alert alert-danger">Invalid Password</div>';
                               set_message($error_msg);
                           }

                       }

                    }
                    else
                        {
                            $error_msg = '<div class="alert alert-danger">Invalid User Name</div>';
                            set_message($error_msg );
                        }
                }
        }
    }

Then you need to make dashboard.php file inside the file have simple cards, If the user enter correct username and password who will redirect with dashboard.php file, otherwise who will able to see error message inside the index.php file.

<?php
session_start();
if(!isset($_SESSION['username']) && !isset($_SESSION['password']))
{
    header("location:index.php");
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>Login Form in Php with Remember Me</title>
</head>
<body class="bg-dark">

<div class="container">
    <div class="row">
        <div class="col-lg-6 m-auto">
            <div class="card mt-5">
                <form  method="POST">
                    <div class="card-header bg-primary mt-5 mb-3">
                        <h4 class="text-white text-center">Dashboard</h4>
                    </div>

                    <div class="card-footer">
                        <a href="logout.php" class="btn btn-success">Logout</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

</body>
</html>

then you need to make a logut.php file, that helps you to redirect the user with the index.php file. If the user logged In your application and clicked on the logout button.

<?php

    session_start();
    
    session_unset($_SESSION['username']);
        session_unset($_SESSION['password']);
        session_destroy();
    
        if(isset($_COOKIE['USER_NAME']))
        {
            unset($_COOKIE['USER_NAME']);
            unset($_COOKIE['PASSWORD']);
            setcookie('USER_NAME','',time()-86400*30);
            setcookie('PASSWORD','',time()-86400*30);
        }
    
         header("location:index.php");

Finally, you need to protect the the index.php page, means If the user logged in the application, then who can’t see the login page inside the index.php page. So, you need to use below mentioned code.

You May Also Like:

    if(isset($_SESSION['username']) && isset($_SESSION['password']))
    {
        header("location:dashboard.php");
    }

However, I also attached the source code of the project. If you have any questions/suggestions feel free to contact me, I will be happy to give a response as soon as possible. Thanks for visiting and watching the tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.