PHP program to demonstrate user login | Login System in PHP

Write a PHP program to give authentication for registered users with Database (Login System in PHP)


In this tutorial, you will learn how to give authentication for registered users in php. So, basically we are going to build a Login system in php for registered users.

Below is the php program for login system in php.

<html>
<head>
    <title>Login System in PHP</title>
</head>
<body>

    <h3>Write a PHP program to give authentication for registered users with Database (Login System)</h3>

    <form action="" method="POST">

        <label>Username</label><br>
        <input type="text" name="username"><br>

        <label>Password</label><br>
        <input type="password" name="password"><br>

        <input type="submit" name="submit">
       
    </form>

    <?php
        $connection = mysqli_connect("localhost","root","");
        $db = mysqli_select_db($connection,'database_name');

        if(isset($_POST['submit']))
        {
            $name = $_POST['username'];
            $pswd = $_POST['password'];

            $query = mysqli_query($connection,"SELECT username,password FROM usersTable WHERE username='$name' and password='$pswd'");

            if($query)
            {
                if(mysqli_num_rows($query) > 0)//returns rows of data from table
                    echo 'Login Success';
                else
                    echo 'Login failed';
            }
        }
    ?>

</body>
</html>


Thanks for reading.