Here's a basic implementation of a responsive PHP attendance system with a beautiful login page and the required features. I'll provide the code structure and key files.
Project Structure:
code Copy code
attendance-system/
├── config.php
├── style.css
├── index.php (Login Page)
├── dashboard.php
├── add-member.php
├── add-member-process.php
├── mark-attendance.php
├── mark-attendance-process.php
├── attendance-list.php
├── logout.php
└── scripts.js
1. Database Setup (MySQL)
sql Copy code
CREATE DATABASE attendance_db;
USE attendance_db;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('admin') DEFAULT 'admin'
);
CREATE TABLE members (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE attendance (
id INT PRIMARY KEY AUTO_INCREMENT,
member_id INT,
date DATE NOT NULL,
status ENUM('present', 'absent') NOT NULL,
FOREIGN KEY (member_id) REFERENCES members(id)
);
2. config.php (Database Connection)
php Copy code
<?php
session_start();
$host = 'localhost';
$db = 'attendance_db';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
// Initialize CSRF token
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
3. index.php (Login Page)
php Copy code
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container d-flex align-items-center vh-100">
<div class="card w-75 mx-auto" style="max-width: 400px;">
<div class="card-body">
<h3 class="text-center mb-4">Attendance System</h3>
<form action="login.php" method="POST">
<div class="mb-3">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<input type="text" class="form-control" name="username" placeholder="Username" required>
</div>
<div class="mb-3">
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
<?php if (isset($_GET['error'])): ?>
<div class="alert alert-danger mt-3">Invalid credentials!</div>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>
4. login.php (Login Handling)
php Copy code
<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_POST['csrf_token'] !== $_SESSION['csrf_token']) {
die("CSRF attack detected!");
}
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: dashboard.php");
exit;
} else {
header("Location: index.php?error=1");
exit;
}
}
?>
5. dashboard.php (Admin Dashboard)
php Copy code
<?php
require 'config.php';
if (!isset($_SESSION['user_id'])) {
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="#">Attendance System</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<div class="row">
<div class="col-md-4 mb-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Add Member</h5>
<a href="add-member.php" class="btn btn-primary">Go</a>
</div>
</div>
</div>
<div class="col-md-4 mb-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Mark Attendance</h5>
<a href="mark-attendance.php" class="btn btn-primary">Go</a>
</div>
</div>
</div>
<div class="col-md-4 mb-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Attendance List</h5>
<a href="attendance-list.php" class="btn btn-primary">Go</a>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
6. add-member.php (Add Member Form)
php Copy code
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Member</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-4">
<h2 class="mb-4">Add New Member</h2>
<form action="add-member-process.php" method="POST">
<div class="mb-3">
<input type="text" class="form-control" name="name" placeholder="Name" required>
</div>
<div class="mb-3">
<input type="email" class="form-control" name="email" placeholder="Email" required>
</div>
<button type="submit" class="btn btn-primary">Add Member</button>
</form>
</div>
</body>
</html>
7. add-member-process.php (Add Member Processing)
php Copy code
<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$stmt = $pdo->prepare("INSERT INTO members (name, email) VALUES (?, ?)");
$stmt->execute([$name, $email]);
header("Location: dashboard.php");
exit;
}
?>
8. mark-attendance.php (Mark Attendance Form)
php Copy code
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mark Attendance</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-4">
<h2 class="mb-4">Mark Attendance</h2>
<form action="mark-attendance-process.php" method="POST">
<div class="mb-3">
<label class="form-label">Date</label>
<input type="date" class="form-control" name="date" required>
</div>
<div class="mb-3">
<label class="form-label">Select Member</label>
<select class="form-select" name="member_id" required>
<option value="">-- Select Member --</option>
<?php
$stmt = $pdo->query("SELECT * FROM members");
while ($member = $stmt->fetch()) {
echo "<option value='{$member['id']}'>{$member['name']}</option>";
}
?>
</select>
</div>
<div class="mb-3">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="status" value="present" required>
<label class="form-check-label">Present</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="status" value="absent" required>
<label class="form-check-label">Absent</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
9. mark-attendance-process.php (Mark Attendance Processing)
php Copy code
<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$member_id = $_POST['member_id'];
$date = $_POST['date'];
$status = $_POST['status'];
$stmt = $pdo->prepare("INSERT INTO attendance (member_id, date, status) VALUES (?, ?, ?)");
$stmt->execute([$member_id, $date, $status]);
header("Location: dashboard.php");
exit;
}
?>
10. attendance-list.php (Attendance List)
php Copy code
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attendance List</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<style>
@media print {
body * { visibility: hidden; }
.print-content * { visibility: visible; }
.print-content { position: absolute; top: 0; left: 0; }
}
</style>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-4">
<h2 class="mb-4">Attendance List</h2>
<button onclick="window.print()" class="btn btn-secondary mb-3">Print List</button>
<div class="print-content">
<table class="table table-bordered">
<thead>
<tr>
<th>Member</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $pdo->query("SELECT a.*, m.name AS member_name FROM attendance a JOIN members m ON a.member_id = m.id");
while ($row = $stmt->fetch()) {
echo "<tr>";
echo "<td>{$row['member_name']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td>{$row['status']}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
11. logout.php (Logout Page)
php Copy code
<?php
session_start();
session_destroy();
header("Location: index.php");
exit;
?>
12. style.css (Custom Styles)
css Copy code
body {
font-family: 'Arial', sans-serif;
}
.card {
background-color: #f8f9fa;
border: none;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.navbar {
padding: 1rem 0;
}
.print-content {
padding: 20px;
}
Usage Instructions:
Database Setup :
Import the SQL schema into your MySQL database.
Update config.php with your database credentials.
First User Setup :
Run this query to add an admin user:
sql Copy code
INSERT INTO users (username, password)
VALUES ('admin', PASSWORD('password'));
Run the Application :
Place all files in your web server directory (e.g., Apache's htdocs).
Access http://localhost/attendance-system/index.php.
Features:
Responsive design using Bootstrap.
Secure login with CSRF protection.
Add members, mark attendance, and view/print attendance lists.
Clean and modern interface.
Let me know if you need further customization or improvements!