Login Page Authentication Tutorial with Backend Using PHP and MySQL

Login Page Authentication Tutorial with Backend Using PHP and MySQL








Login.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
</head>
<body>
    <h1 class="Login">Login Page</h1>

    <div class="container">
        <form action="login_authentication.php" method="post">

        <label for="username">username:</label>
        <input type="text" name="username" id="username">
        <br>
        <br>
        <label for="password">password:</label>
        <input type="password" name="password" id="password">
        <br>
        <br>
        <button type="submit" name="login_authentication">Login</button>
        
        </form>
    </div>
</body>
</html>
login_authentication.php
<?php
session_start();

$servername = "localhost";
$username ="root";
$password="";
$dbname="huntexams";

$conn = mysqli_connect("$servername","$username","$password","$dbname");

if(!$conn)
{
    echo "fail";
}


if(isset($_POST['login_authentication']))
{
     $user = $_POST['username'];
     $pass = $_POST['password'];
}

$query = "SELECT username, password From login where username = '$user' and password = '$pass'";

$result  = mysqli_query($conn,$query);

if(mysqli_num_rows($result) == 1)
{
    header("Location:welcome.php");
}
else{
    echo "Invalid details!! please enter correct details..!!";
}
?>
welcome.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>welcome</title>
</head>
<body>
    <center><h1>Welcome to this webpage</h1></center>
    <center>Login Successful!!</center>
<br>
    <center>
        <a href="logout.php">
        <button type="button">Logout</button>
        </a>
</center>
</body>
</html>

logout.php

  
  <?php
session_start();

session_destroy();

header("Location:login.php");

?>



Previous Post Next Post

Contact Form