Spaces:
Running
Running
| ```php | |
| require_once 'database_connection.php'; | |
| session_start(); | |
| if (isset($_SESSION['admin_logged_in'])) { | |
| header('Location: admin_dashboard.php'); | |
| exit; | |
| } | |
| $error = ''; | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| $username = $_POST['username'] ?? ''; | |
| $password = $_POST['password'] ?? ''; | |
| $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?"); | |
| $stmt->execute([$username]); | |
| $admin = $stmt->fetch(); | |
| if ($admin && password_verify($password, $admin['password'])) { | |
| $_SESSION['admin_logged_in'] = true; | |
| header('Location: admin_dashboard.php'); | |
| exit; | |
| } else { | |
| $error = 'Invalid username or password'; | |
| } | |
| } | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Admin Login</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; } | |
| .login-form { border: 1px solid #ddd; padding: 20px; border-radius: 5px; } | |
| .form-group { margin-bottom: 15px; } | |
| label { display: block; margin-bottom: 5px; } | |
| input { width: 100%; padding: 8px; box-sizing: border-box; } | |
| button { background: #007bff; color: white; border: none; padding: 10px 15px; cursor: pointer; } | |
| .error { color: red; margin-bottom: 15px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Admin Login</h1> | |
| if ($error): | |
| <div class="error"><?= htmlspecialchars($error) ?></div> | |
| <?php endif; ?> | |
| <div class="login-form"> | |
| <form method="POST"> | |
| <div class="form-group"> | |
| <label for="username">Username:</label> | |
| <input type="text" id="username" name="username" required> | |
| </div> | |
| <div class="form-group"> | |
| <label for="password">Password:</label> | |
| <input type="password" id="password" name="password" required> | |
| </div> | |
| <button type="submit">Login</button> | |
| </form> | |
| </div> | |
| </body> | |
| </html> | |
| ``` |