= 2006 && $firstFourDigits <= 2025) { return ['valid' => $cleanedInput]; } else { return ['invalid' => $cleanedInput, 'reason' => 'First four digits are not within 2006-2025']; } } else { return ['invalid' => $cleanedInput, 'reason' => 'Not 17 digits long']; } } $validResults = []; $invalidResults = []; $error = null; // Validate the request method and CSRF token if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) { $error = "Invalid CSRF token."; } else { $inputData = filter_input(INPUT_POST, 'input_data', FILTER_SANITIZE_STRING); $rows = explode("\n", trim($inputData)); foreach ($rows as $row) { $result = processInput($row); if ($result && isset($result['valid'])) { $validResults[] = $result['valid']; } elseif ($result) { $invalidResults[] = $result; } } // Remove duplicate valid results $validResults = array_unique($validResults); } } // Function to create a downloadable file link function createDownloadLink($filename, $data) { $filepath = sys_get_temp_dir() . '/' . $filename; file_put_contents($filepath, implode("\n", $data)); return $filepath; } // Handle the download of valid results if (isset($_POST['download_valid']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) { $filepath = createDownloadLink('valid_results.txt', $validResults); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="valid_results.txt"'); readfile($filepath); exit; } // Handle the download of invalid results if (isset($_POST['download_invalid']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) { $invalidOutput = array_map(function ($item) { return $item['invalid'] . " - " . $item['reason']; }, $invalidResults); $filepath = createDownloadLink('invalid_results.txt', $invalidOutput); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="invalid_results.txt"'); readfile($filepath); exit; } ?>
Input Rows: = count($rows) ?>
Valid Output Rows (Unique): = count($validResults) ?>
Invalid/Discarded Rows: = count($invalidResults) ?>
0): ?>