$backUrl ="/login"; $backUrl = $backUrl ?? null; $pageTitle = 'Reset Password'; ?> session_start(); include("includes/connection.php"); require_once __DIR__ . '/../config/smtp.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $email = trim($_POST['email']); $stmt = $mysqli->prepare("SELECT id FROM tbl_users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $stmt->bind_result($user_id); $stmt->fetch(); $token = bin2hex(random_bytes(32)); $tokenHash = password_hash($token, PASSWORD_DEFAULT); $expiresAt = date("Y-m-d H:i:s", strtotime("+1 hour")); $update = $mysqli->prepare("UPDATE tbl_users SET reset_token = ?, token_expiry = ? WHERE id = ?"); $update->bind_param("ssi", $tokenHash, $expiresAt, $user_id); $update->execute(); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://"; $host_url = $_SERVER['HTTP_HOST']; $path = dirname($_SERVER['REQUEST_URI'], 1); $resetLink = $protocol . $host_url . $path . "reset-password.php?token=$token"; $templateKey = 'password_reset'; $templateStmt = $mysqli->prepare("SELECT subject, body FROM tbl_email_templates WHERE template_key = ? AND is_active = 1 LIMIT 1"); $templateStmt->bind_param("s", $templateKey); $templateStmt->execute(); $templateStmt->store_result(); if ($templateStmt->num_rows > 0) { $templateStmt->bind_result($subject, $body); $templateStmt->fetch(); $message = str_replace("{{RESET_LINK}}", $resetLink, $body); $domain = parse_url((!empty($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'], PHP_URL_HOST); $fromEmail = "no-reply@" . preg_replace('/^www\./', '', $domain); try { $mail = getMailer(); $mail->setFrom($fromEmail, $fromEmail); $mail->addAddress($email); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = strip_tags($message); $mail->send(); } catch (Exception $e) { error_log("Failed to send reset email to $email: " . $e->getMessage()); } } else { error_log("Email template '$templateKey' not found or inactive."); } } $_SESSION['msg'] = "If your email exists in our system, a reset link has been sent."; header("Location: forgot-password.php"); exit; } $backUrl ="/login"; $backUrl = $backUrl ?? null; $pageTitle = 'Reset Password'; ?> Forgot Password

Forgot Your Password?

Enter your email address and we’ll send you a link to reset your password.