Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Program 8

Aim: Write a PHP script for login authentication. Design an HTML form which takes
username and password from user and validate against stored username and password in
file.

Aman Malik
IT-E
00496303121
Code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
</head>
<body>
<?php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$username = $_POST["username"];
$password = $_POST["password"];

// Validate against stored credentials (for demonstration purposes)


$stored_username = "myuser"; // Replace with your actual stored username
$stored_password = "mypassword"; // Replace with your actual stored password

if ($username === $stored_username && $password === $stored_password) {


// Successful login
echo "Welcome, $username!";
} else {
// Invalid credentials
echo "Invalid username or password. Please try again.";
}

Aman Malik
IT-E
00496303121
}
?>

<h1>Login Form</h1>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>

<input type="submit" value="Login">


</form>
</body>
</html>

Output:

Aman Malik
IT-E
00496303121

You might also like