<?php
session_start();

$userOtp = $_POST['otp_input'] ?? '';

if (!isset($_SESSION['otp'])) {
    $_SESSION['error'] = "No OTP session found. Please reload the page.";
    header("Location: otplogin.php");
    exit;
}

if (time() > $_SESSION['otp_expires']) {
    unset($_SESSION['otp']);
    $_SESSION['error'] = "OTP expired. Please reload the page.";
    header("Location: otplogin.php");
    exit;
}

if ($userOtp === $_SESSION['otp']) {
    unset($_SESSION['otp']);
    header("Location: banking-dashboard/dashboard.php");
    exit;
} else {
    $_SESSION['error'] = "Wrong OTP. Please try again.";
    header("Location: otplogin.php");
    exit;
}
