fork download
  1. <?php
  2. // Check if the form is submitted
  3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4. // Retrieve the entered username and password from the POST request
  5. $enteredUsername = $_POST['username'];
  6. $enteredPassword = $_POST['password'];
  7. // Check if the cookies for username and password are set
  8. if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
  9. // Retrieve the username and password from the cookies
  10. $cookieUsername = $_COOKIE['username'];
  11. $cookiePassword = $_COOKIE['password'];
  12. // Authenticate the entered credentials with the cookies' values
  13. if ($enteredUsername === $cookieUsername && $enteredPassword ===
  14. $cookiePassword) {
  15. // If valid, welcome the user by username
  16. echo "Welcome, " . htmlspecialchars($enteredUsername) . "!";
  17. } else {
  18. // If invalid, display an error message
  19. echo "Invalid username or password. Please try again.";
  20. }
  21. } else {
  22. // If cookies are not set, display a message
  23. echo "No credentials stored. Please register or log in.";
  24. }
  25. }
  26. ?>
  27.  
  28. <!-- Sample HTML Login Form -->
  29. <!DOCTYPE html>
  30. <html lang="en">
  31. <head>
  32. <meta charset="UTF-8">
  33. <title>Online Book Store Login</title>
  34. </head>
  35. <body>
  36. <h2>Login</h2>
  37. <form method="POST" action="">
  38. <label for="username">Username:</label>
  39. <input type="text" id="username" name="username" required><br><br>
  40. <label for="password">Password:</label>
  41. <input type="password" id="password" name="password"
  42. required><br><br>
  43. <button type="submit">Login</button>
  44. </form>
  45. </body>
  46. </html>
  47.  
Success #stdin #stdout 0.02s 25616KB
stdin
Standard input is empty
stdout
<?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>