prepare("SELECT * FROM email_verifications WHERE user_id = ? AND verification_code = ?"); $stmt->bind_param("is", $userId, $verification_code); $stmt->execute(); $result = $stmt->get_result(); $record = $result->fetch_assoc(); $stmt->close(); if (!$record) { $_SESSION['error'] = "Invalid verification code."; header("Location: profile.php"); exit; } // Check if the verification code has expired $current_time = new DateTime(); $expires_at = new DateTime($record['expires_at']); if ($current_time > $expires_at) { $_SESSION['error'] = "Verification code has expired. Please request a new one."; header("Location: profile.php"); exit; } // Verification successful: update the user's record $stmt = $db->prepare("UPDATE users SET emailVerified = 1 WHERE id = ?"); $stmt->bind_param("i", $userId); $stmt->execute(); $stmt->close(); // Remove the verification record for cleanup $stmt = $db->prepare("DELETE FROM email_verifications WHERE user_id = ?"); $stmt->bind_param("i", $userId); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Email verified successfully."; header("Location: profile.php"); exit;