this is my current test.php code
<?php
$years = [2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023]; // Array of years to include
$sessions = [
"MJ" => "May/June (S)",
"FM" => "March (FM)",
"ON" => "October/November (W)"
];
$qps = [11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43, 51, 52, 53, 61, 62, 63]; // QP codes
$mss = [11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43, 51, 52, 53, 61, 62, 63]; // MS codes for May/June and October/November
$mss_march = [12, 22, 32, 42, 52, 62]; // MS codes for March
$cache_file = 'cache.json';
$cache_duration = 3600; // Cache duration in seconds (1 hour)
function generateLinks($year, $session, $codes, $type, $vertical = false) {
global $cache_file, $cache_duration;
$year_suffix = substr($year, -2);
$session_prefix = strtolower($session) === "mj" ? "s" : (strtolower($session) === "fm" ? "m" : "w");
$links = "";
$cache = loadCache($cache_file);
if ($vertical) {
foreach ($codes as $code) {
$url = "https://pp.great-site.net/0610/{$year}/{$session}/0610_{$session_prefix}{$year_suffix}_{$type}_{$code}.pdf";
if (urlExists($url, $cache)) {
$links .= "<div><a href="{$url}" target="_blank">{$code}</a></div>";
} else {
$links .= "<div><span style='color:red;'>{$code} (Not Available)</span></div>";
}
}
} else {
foreach (array_chunk($codes, 3) as $chunk) {
$link_block = "";
foreach ($chunk as $code) {
$url = "https://pp.great-site.net/0610/{$year}/{$session}/0610_{$session_prefix}{$year_suffix}_{$type}_{$code}.pdf";
if (urlExists($url, $cache)) {
$link_block .= "<a href="{$url}" target="_blank">{$code}</a> ";
} else {
$link_block .= "<span style='color:red;'>{$code} (Not Available)</span> ";
}
}
$links .= "<div>{$link_block}</div>";
}
}
saveCache($cache_file, $cache);
return $links;
}
function urlExists($url, &$cache) {
global $cache_duration;
if (isset($cache[$url]) && (time() - $cache[$url]['timestamp'] < $cache_duration)) {
return $cache[$url]['exists'];
}
$context = stream_context_create(['http' => ['method' => 'GET', 'timeout' => 10]]);
$headers = @file_get_contents($url, false, $context);
$http_code = $headers ? '200' : '404';
// Debugging information
error_log("Checking URL: $url - HTTP Code: $http_code");
$exists = $http_code == '200';
$cache[$url] = ['exists' => $exists, 'timestamp' => time()];
return $exists;
}
function loadCache($cache_file) {
if (file_exists($cache_file)) {
$cache = json_decode(file_get_contents($cache_file), true);
if (is_array($cache)) {
return $cache;
}
}
return [];
}
function saveCache($cache_file, $cache) {
file_put_contents($cache_file, json_encode($cache));
}
function generateTableRows($years, $sessions, $qps, $mss, $mss_march) {
foreach ($years as $year) {
foreach ($sessions as $session_key => $session_value) {
echo "<tr>";
echo "<td>{$year}</td>";
echo "<td>{$session_value}</td>";
echo "<td>" . generateLinks($year, $session_key, $qps, 'qp') . "</td>";
echo "<td>" . generateLinks($year, $session_key, $session_key === 'FM' ? $mss_march : $mss, 'ms') . "</td>";
echo "</tr>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../../Styles/Bootstrap.css">
<link rel="stylesheet" href="../../Styles/General.css">
<link rel="stylesheet" href="../../Styles/Background.css">
<link rel="stylesheet" href="../../Styles/Tables.css">
<link rel="icon" href ="../../favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon" />
<title>Chemistry (0620)</title>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NHCZKLG8F3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-NHCZKLG8F3');
</script>
</head>
<body class="Grey">
<a href="javascript:window.history.back();" class="btn btn-dark">Back</a>
<h1 class="Center">Chemistry (0620)</h1>
<div class="container table-responsive">
<table class="table table-dark table-bordered">
<thead>
<tr>
<td>Year</td>
<td>Session</td>
<td>Question Paper</td>
<td>Mark Scheme</td>
</tr>
</thead>
<tbody>
<?php generateTableRows($years, $sessions, $qps, $mss, $mss_march); ?>
</tbody>
</table>
</div>
</body>
</html>
i have been trying many different methods to validate the links as where if the link is not available for example for QP 63 it wont appear however all the links are returned as not available with all the different approaches tried 
while this is how it is supposed to be normally
but just without the 63 link that is not available for example
i am new to this but i have been trying many different approaches and even checked chatgpt for help however it kept taking me into loops of using different methods such as get_headers and curl and GET and HEAD however i keep getting the same results, i am currently using infinityfree free hosting service i am using 2 servers one for these files and codes etc. and the other one is solely for the pdf files which i am trying to make links to
the log file generated shows this,
the website containing the pdf files is
https://pp.great-site.net/9700/2023/MJ/9700_s23_ms_42.pdf
this is an example of one of the pdf links since the whole server is just pdf files
Edit: this page is made so that there is a table to show “past paper exams of each year for ig students” every year there are 3 sessions February FM summer S winter (ON) in each session there are papers with different variants for example 63 means paper 6 variant 3 these links should lead to the pdf file with that exam stored on another server/website which i provided the link to so since not all exams are always available i want to carry automated checking if the file exists on the server show the link if it doesnt then delete/hide the link however using the code above it returns all the links as not available although they are valid so my question is what am i doing wrong here that returns all links as not available instead of only the invalid/not available links, the links are created with the help of php automatically as shown in the code above creating a loop for each year i write in the $years. The php code also contains a not working try to make the automatic link validation thing im trying to achieve
Any help would be appreciated, thanks in advance!