37684-vm/reset-password.php
2026-03-01 00:04:22 +00:00

103 lines
4.8 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$project_name = $_SERVER['PROJECT_NAME'] ?? 'LPA Online';
$token = $_GET['token'] ?? ($_POST['token'] ?? '');
$error = '';
$success = '';
if (empty($token)) {
header('Location: login.php');
exit;
}
$stmt = db()->prepare('SELECT id FROM users WHERE reset_token = ? AND reset_expires_at > NOW()');
$stmt->execute([$token]);
$user = $stmt->fetch();
if (!$user) {
$error = 'The reset link is invalid or has expired. Please request a new one.';
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
if (empty($password) || empty($confirm_password)) {
$error = 'Please fill in both password fields.';
} elseif ($password !== $confirm_password) {
$error = 'Passwords do not match.';
} elseif (strlen($password) < 8) {
$error = 'Password must be at least 8 characters long.';
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$update = db()->prepare('UPDATE users SET password = ?, reset_token = NULL, reset_expires_at = NULL WHERE id = ?');
$update->execute([$hashed_password, $user['id']]);
$success = 'Your password has been successfully reset. You can now log in with your new password.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Set New Password — <?php echo htmlspecialchars($project_name); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/custom.css" rel="stylesheet">
</head>
<body class="bg-light d-flex flex-column min-vh-100">
<nav class="navbar navbar-expand-lg bg-white border-bottom shadow-sm">
<div class="container justify-content-center">
<a class="navbar-brand d-flex align-items-center m-0" href="/">
<img src="assets/pasted-20260228-235417-eedda424.png" alt="<?php echo htmlspecialchars($project_name); ?>" height="40">
</a>
</div>
</nav>
<main class="container py-5 flex-grow-1 d-flex align-items-center">
<div class="row justify-content-center w-100 m-0">
<div class="col-md-5 col-lg-4">
<div class="card shadow-lg border-0 p-4 p-md-5 rounded-4">
<div class="text-center mb-4">
<h1 class="h3 fw-bold">Set New Password</h1>
<p class="text-muted small">Please enter your new password below.</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger small py-2"><?php echo htmlspecialchars($error); ?></div>
<div class="text-center mt-3">
<a href="forgot-password.php" class="btn btn-outline-primary btn-sm rounded-pill px-4">Request New Link</a>
</div>
<?php elseif ($success): ?>
<div class="alert alert-success small py-2"><?php echo htmlspecialchars($success); ?></div>
<div class="text-center mt-3">
<a href="login.php" class="btn btn-primary btn-sm rounded-pill px-4">Log In Now</a>
</div>
<?php else: ?>
<form action="reset-password.php" method="POST">
<input type="hidden" name="token" value="<?php echo htmlspecialchars($token); ?>">
<div class="mb-3">
<label for="password" class="form-label small fw-bold">New Password</label>
<input type="password" class="form-control form-control-lg" id="password" name="password" placeholder="••••••••" required minlength="8">
</div>
<div class="mb-4">
<label for="confirm_password" class="form-label small fw-bold">Confirm New Password</label>
<input type="password" class="form-control form-control-lg" id="confirm_password" name="confirm_password" placeholder="••••••••" required minlength="8">
</div>
<button type="submit" class="btn btn-primary btn-lg w-100 rounded-pill">Reset Password</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</main>
<footer class="py-4 text-center text-muted mt-auto">
<div class="container">
<p class="small mb-0">&copy; <?php echo date('Y'); ?> <?php echo htmlspecialchars($project_name); ?>. All rights reserved.</p>
</div>
</footer>
</body>
</html>