login.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. require_once '../includes/config.php';
  3. require_once '../includes/database.php';
  4. require_once '../includes/auth.php';
  5. $auth = new Auth();
  6. if ($auth->isLoggedIn()) {
  7. header('Location: index.php');
  8. exit;
  9. }
  10. $errors = [];
  11. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  12. $username = trim($_POST['username'] ?? '');
  13. $password = $_POST['password'] ?? '';
  14. if (empty($username) || empty($password)) {
  15. $errors[] = 'Please enter both username and password';
  16. } elseif ($auth->login($username, $password)) {
  17. header('Location: index.php');
  18. exit;
  19. } else {
  20. $errors[] = 'Invalid username or password';
  21. }
  22. }
  23. ?>
  24. <!DOCTYPE html>
  25. <html lang="en">
  26. <head>
  27. <meta charset="UTF-8">
  28. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29. <title>Admin Login - <?php echo SITE_TITLE; ?></title>
  30. <link rel="stylesheet" href="../css/style.css">
  31. </head>
  32. <body class="login-page">
  33. <div class="container">
  34. <div class="login-form">
  35. <h1>Admin Login</h1>
  36. <?php if (!empty($errors)): ?>
  37. <div class="alert alert-error">
  38. <?php foreach ($errors as $error): ?>
  39. <p><?php echo htmlspecialchars($error); ?></p>
  40. <?php endforeach; ?>
  41. </div>
  42. <?php endif; ?>
  43. <form method="post">
  44. <div class="form-group">
  45. <label for="username">Username:</label>
  46. <input type="text" id="username" name="username" required>
  47. </div>
  48. <div class="form-group">
  49. <label for="password">Password:</label>
  50. <input type="password" id="password" name="password" required>
  51. </div>
  52. <button type="submit" class="btn btn-primary">Login</button>
  53. </form>
  54. </div>
  55. </div>
  56. </body>
  57. </html>