Here, we will write a program to create login page using PHP in XAMPP software. your login.php file should be inside the htdocs folder which is inside Xampp directory. We will also create a logout.php file here for additional functionality. After writing the code start your apache server and run the file in browser under localhost.
Login.php file
<?php
session_start();
$db=mysqli_connect("localhost","root","",) or die(mysqli_error($db));
mysqli_select_db($db,"registerdb") or die(mysqli_error($db));
if(isset($_POST["submit"])){
$username=$_POST["username"];
$pwd=$_POST["pwd"];
$res=mysqli_query($db,"select * from login where user_name='$username' && password='$pwd'");
$row=mysqli_num_rows($res);
if($row>0){
$_SESSION['user']=$username;
header("location:profileList.php");
}
else{
echo "error";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="login-form">
<div class="container mt-3">
<h2>Login form</h2>
<form action="" method="post">
<div class="mb-3 mt-3">
<label for="username">User Name:</label>
<input type="username" class="form-control" id="email" placeholder="Enter user name" name="username">
</div>
<div class="mb-3">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
<button type="submit" name="submit" class="btn btn-primary">Login</button>
</form>
</div>
</div>
</body>
</html>
logout.php file
<?php
session_start();
$_SESSION['user']="";
unset($_SESSION['user']);
header('location:login.php');
?>