<?php
session_start();
require 'config.php';

$error = '';

// If user already logged in, send to correct dashboard
if (isset($_SESSION['username']) && isset($_SESSION['role'])) {
    if ($_SESSION['role'] === 'admin') {
        header("Location: admin_dashboard.php");
    } else {
        header("Location: member_dashboard.php");
    }
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username === '' || $password === '') {
        $error = "Please fill both username and password.";
    } else {
        // Check user in DB
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            // Save user info in session
            $_SESSION['username'] = $user['username'];
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];

            // Redirect depending on role
            if ($user['role'] === 'admin') {
                header("Location: admin_dashboard.php");
            } else {
                header("Location: member_dashboard.php");
            }
            exit;
        } else {
            $error = "Invalid username or password.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Library LMS - Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <!-- Optional Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet" />
    <style>
        body, html {
            height: 100%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            font-family: 'Poppins', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #fff;
        }
        .login-container {
            background: rgba(255, 255, 255, 0.1);
            padding: 40px 50px;
            border-radius: 15px;
            box-shadow: 0 15px 40px rgba(0,0,0,0.3);
            width: 100%;
            max-width: 420px;
            backdrop-filter: blur(10px);
            animation: fadeIn 1s ease forwards;
        }
        h2 {
            font-weight: 600;
            margin-bottom: 30px;
            text-align: center;
            letter-spacing: 1.5px;
            text-shadow: 0 2px 8px rgba(0,0,0,0.3);
        }
        label {
            font-weight: 600;
            letter-spacing: 0.8px;
            color: #e0e0e0;
        }
        .form-control {
            background: rgba(255,255,255,0.2);
            border: none;
            border-radius: 10px;
            color: #fff;
            padding: 15px 20px;
            font-size: 1rem;
            box-shadow: inset 2px 2px 4px rgba(255,255,255,0.3);
            transition: background-color 0.3s ease;
        }
        .form-control:focus {
            background: rgba(255,255,255,0.4);
            color: #222;
            box-shadow: 0 0 10px #9155fd;
            outline: none;
        }
        .btn-primary {
            background: #9155fd;
            border: none;
            padding: 15px 0;
            font-weight: 600;
            font-size: 1.1rem;
            border-radius: 10px;
            width: 100%;
            transition: background-color 0.3s ease;
            box-shadow: 0 5px 15px rgba(145,85,253,0.6);
        }
        .btn-primary:hover {
            background: #682bdf;
            box-shadow: 0 8px 25px rgba(104,43,223,0.7);
        }
        .alert-danger {
            background-color: rgba(255, 72, 66, 0.85);
            border-radius: 8px;
            padding: 12px 20px;
            font-weight: 600;
            margin-bottom: 20px;
            text-align: center;
            box-shadow: 0 3px 10px rgba(255,72,66,0.5);
        }
        @keyframes fadeIn {
            0% {opacity: 0; transform: translateY(20px);}
            100% {opacity: 1; transform: translateY(0);}
        }
        ::placeholder {
            color: rgba(255,255,255,0.7);
            font-weight: 400;
        }
    </style>
</head>
<body>
<div class="login-container shadow-lg">
    <h2>Library LMS Login</h2>
    <?php if ($error): ?>
        <div class="alert alert-danger"><?=htmlspecialchars($error)?></div>
    <?php endif; ?>
    <form method="post" novalidate autocomplete="off">
        <div class="mb-4">
            <label for="username" class="form-label">Username</label>
            <input id="username" type="text" name="username" class="form-control" placeholder="Enter your username" required value="<?=htmlspecialchars($_POST['username'] ?? '')?>" />
        </div>
        <div class="mb-5">
            <label for="password" class="form-label">Password</label>
            <input id="password" type="password" name="password" class="form-control" placeholder="Enter your password" required />
        </div>
        <button type="submit" class="btn btn-primary">Log In</button>
    </form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>  after in puting admin user and pass it shows  This page isn’t working
localhost redirected you too many times.

Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS  and http://localhost/library_lms/admin_dashboard.php