36352-vm/login.php
Flatlogic Bot a2059511fc t4
2025-11-27 10:08:20 +00:00

103 lines
3.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$db = db();
// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
header("Location: index.php");
exit();
}
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$error_message = 'Please enter both username and password.';
} else {
$stmt = $db->prepare("SELECT id, password, role_id FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $username;
$_SESSION['role_id'] = $user['role_id'];
// Log attendance
$login_time = date('Y-m-d H:i:s');
$ip_address = $_SERVER['REMOTE_ADDR'];
$attendance_stmt = $db->prepare("INSERT INTO attendance (user_id, login_time, ip_address) VALUES (:user_id, :login_time, :ip_address)");
$attendance_stmt->bindParam(':user_id', $user['id']);
$attendance_stmt->bindParam(':login_time', $login_time);
$attendance_stmt->bindParam(':ip_address', $ip_address);
$attendance_stmt->execute();
$_SESSION['attendance_id'] = $db->lastInsertId();
header("Location: index.php");
exit();
} else {
$error_message = 'Invalid username or password.';
}
}
}
// Fetch header content
ob_start();
include 'index.php';
$page_content = ob_get_clean();
// Extract only the <head> and <header>
$head_and_header = '';
if (preg_match('/<head>.*?<\/head>/s', $page_content, $head_match)) {
$head_and_header .= $head_match[0];
}
if (preg_match('/<header>.*?<\/header>/s', $page_content, $header_match)) {
$head_and_header .= $header_match[0];
}
// Replace active nav link
$head_and_header = str_replace('<a class="nav-link" href="index.php">Home</a>', '<a class="nav-link" href="index.php">Home</a>', $head_and_header);
$head_and_header = preg_replace('/<a class="nav-link active" (.*?)>/', '<a class="nav-link" $1>', $head_and_header);
echo str_replace('</head>', '<style>.login-container { max-width: 400px; margin: 5rem auto; }</style></head>', $head_and_header);
?>
<main class="container mt-5">
<div class="login-container card">
<div class="card-body">
<h1 class="card-title text-center mb-4">Login</h1>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form action="login.php" method="post">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
</div>
</div>
</main>
<?php
// Extract footer
if (preg_match('/<footer.*?>.*?<\/footer>/s', $page_content, $footer_match)) {
echo $footer_match[0];
}
?>