0% found this document useful (0 votes)
14 views2 pages

WT 8

Uploaded by

Harsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

WT 8

Uploaded by

Harsh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM-8: 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.

CODE:

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
function validateLogin($username, $password, $file_path) {
$lines = file($file_path, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
$data = explode(':', $line);
if ($data[0] === $username && $data[1] === $password) {
return true;
}
}
return false;
}

$file_path = 'users.txt';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
if (validateLogin($username, $password, $file_path)) {
echo "<h2>Login successful!</h2>";
} else {
echo "<h2>Login failed. Invalid username or password.</h2>";
}
}
?>

19
OUTPUT:

20

You might also like