<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the entered username and password from the POST request
$enteredUsername = $_POST['username'];
$enteredPassword = $_POST['password'];
// Check if the cookies for username and password are set
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) { // Retrieve the username and password from the cookies
$cookieUsername = $_COOKIE['username'];
$cookiePassword = $_COOKIE['password'];
// Authenticate the entered credentials with the cookies' values
if ($enteredUsername === $cookieUsername && $enteredPassword ===
$cookiePassword) {
// If valid, welcome the user by username
echo "
;Welcome
, "
; . htmlspecialchars($enteredUsername) . "
;!"
;; } else {
// If invalid, display an error message
echo "Invalid username or password. Please try again.";
}
} else {
// If cookies are not set, display a message
echo "
;No credentials stored
. Please register or
log in
."
;; }
}
?>
<!-- Sample HTML Login Form -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Online Book Store Login</title>
</head>
<body>
<h2>Login</h2>
<form method="POST" action="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"
required><br><br>
<button type="submit">Login</button>
</form>
</body>
</html>