I am using require functions as following:
<?php
require("../require/head.php");
require("../require/header.php");
require("content.php");
require("../require/footer.php");
?>
Is it possible to throw 503 code if any require doesn't find its target and how? Also is it possible to throw normal exception where I don't want 503 error and how?
Because the HTTP server response code is sent in the headers, you must determine this before any payload is sent to the client.
So you could either use output buffering or first loop over the required files checking readability, then use http_response_code
to send 503 if one of the resources are missing.
<?php
$files = [
"../require/head.php",
"../require/header.php",
"content.php",
"../require/footer.php",
];
foreach ($files as $file) {
if (!is_readable($file)) {
// return 503
http_response_code(503);
die();
// or you could throw an exception
throw new Exception("Missing file: $file");
}
}
foreach ($files as $file) {
require $file;
}