How to Make Login Form in PHP with Session and MySQL

0

Guys, Hope You All of fine, in this tutorial we will learn How to Make a Login Form in PHP with Session and MySQL. Guys, if you are working on PHP Programming language and you want to learn how you can use Session to Develop Simple Login Form with Database. I’m going to share with you my personal experience, that helps you to understand make a Login Form in PHP using session.

Login-Form-in-PHP-with-Session-and-MySQL

Basically, If you are using Facebook, Twitter, YouTube, or Any Other Popular Web Application, the popular application has two features namely registration and also log-in. If you are using Any application which is mentioned here, you need to create an account, once you do that, then you need to log in to the application.

once you login to the application, then you redirected to Home Page or Another page If you closed your Browser Tab or Web Application. After Closing the Web Application, you opened again Same Web Application, you didn’t enter User Name And Password, you were redirected to Home Page right.

Guys, In the Back-end Session running, If you logged Out With your Account, then you can’t access Home, you need to Enter the User Name & Password because after clicking the Logout Button Session was Stopped. So, in this tutorial, you will learn same thing which is included popular web applications.

You May Also Like: School Managment System in PHP

I will teach you how you can do that? Guys, First of All, you need to Design a Login Form with the help of HTML5, CSS3, or Bootstrap. Basically, I designed Login Form Using Bootstrap, you can use Bootstrap to design an attractive, Professional, and Responsive Login Form.

Login Form in Php Using Session

First of all we need to design the login form using HTML/CSS or Bootstrap. I’ve used Bootstrap to design the login form, inside my index.php file have bootstrap classes that help you to design the page structure and also login form, you can checkout the codes on below.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-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 Session</title>
</head>
<body style="background:#CCC;">

    <div class="container">
        <div class="row">
            <div class="col-lg-6 m-auto">
                <div class="card bg-dark mt-5">
                    <div class="card-title bg-primary text-white mt-5">
                        <h3 class="text-center py-3">Login Form in PHP </h3>
                    </div>
                    <div class="card-body">
                        <form action="process.php" method="post">
                            <input type="text" name="UName" placeholder=" User Name" class="form-control mb-3">
                            <input type="password" name="Password" placeholder=" Password" class="form-control mb-3">
                            <button class="btn btn-success mt-3" name="Login">Login</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

Then, You need to Create Connection.PHP File. This file has a Connection with PHP with MySQLi Database. Below I mentioned codes.

You May Also Like: Login Form in PHP With Source Code

Php MySQLi Connection Code:

mysqli_connect(host, username, password, dbname)
<?php

    $con=mysqli_connect('localhost','root','','phptutorials');

    if(!$con)
    {
        die(' Please Check Your Connection'.mysqli_error($con));
    }
?>

Then You need to create Process.PHP File, In this, Filed, you need to Get Data From Input Fileds and Also Some Security Codes such as Empty Input Fields and Also Direct Files like Wellcome.PHP.

Select Statement Using Where Keyword which used to check the email exists or not checkout the mysqli query on below.

SELECT column_name(s) FROM table_name WHERE column_name operator value 
<?php 
require_once('connection.php');
session_start();
    if(isset($_POST['Login']))
    {
       if(empty($_POST['UName']) || empty($_POST['Password']))
       {
            header("location:index.php?Empty= Please Fill in the Blanks");
       }
       else
       {
            $query="select * from employee where UName='".$_POST['UName']."' and Pass='".$_POST['Password']."'";
            $result=mysqli_query($con,$query);

            if(mysqli_fetch_assoc($result))
            {
                $_SESSION['User']=$_POST['UName'];
                header("location:wellcome.php");
            }
            else
            {
                header("location:index.php?Invalid= Please Enter Correct User Name and Password ");
            }
       }
    }
    else
    {
        echo 'Not Working Now Guys';
    }

?>

After creating all files, you need to create more than two files namely Wellcome.PHP And Also Logout.PHP. I mentioned below Wellcome.PHP Code.

<?php
    session_start();

    if(isset($_SESSION['User']))
    {
        echo ' Well Come ' . $_SESSION['User'].'<br/>';
        echo '<a href="logout.php?logout">Logout</a>';
    }
    else
    {
        header("location:index.php");
    }

?>

The last One is Logout.PHP In this File you can see user Click on Logout Link, Then User Redirect with Home Page or Index.PHP. File that’s it, Guys.

<?php 
    session_start();
    if(isset($_GET['logout']))
    {
        session_destroy();
        header("location:index.php");
    }

?>

Finally, you need to use below mentioned codes inside the index.php file. You can use that before the card-body tag, once you add that, then you will able to see error messages will appear on your same page after the Heading text. hope you understand that.

<?php 
                        if(@$_GET['Empty']==true)
                        {
                    ?>
                        <div class="alert-light text-danger text-center py-3"><?php echo $_GET['Empty'] ?></div>                                
                    <?php
                        }
                    ?>


                    <?php 
                        if(@$_GET['Invalid']==true)
                        {
                    ?>
                        <div class="alert-light text-danger text-center py-3"><?php echo $_GET['Invalid'] ?></div>                                
                    <?php
                        }
                    ?>

Above all about Login Form in PHP with Session and MySQL. You need to follow the above process, then you are able to make a Login Form in PHP with Session and MySQL.

You May Also Like: Best PHP Scripts With Source Codes

Furthermore, I have mentioned below a Video tutorial that helps you with How to Make a Login Form in PHP with Session and MySQL step by step. I hope the video is helpful and beneficial for everyone to understand.

Login Form in PHP with Session and MySQL

Guys, I hope you have learned something new in this video.  I have mentioned each way to learn Login Form in PHP with Session and MySQL. If you like it, kindly share and like the content and Share with Social Networks.

If you have any question/suggestion, comment me or email, after receiving your email. I will help you to solve your problems. Thanks For Watching and Read the Complete Article.

Kindly Subscribe My YouTube Channel For Getting New Video Tutorials. Please Subscribe My YouTube Channel

Previous articlePhp Booking System with Source Code
Next articleHow to Make Login System in PHP with Validation
I'm Programmer, Digital Marketer, and Blogger, I have been working on the web for 04 years. Basically, I have been sharing personal expertise on my website.

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.