How can PHP developers ensure that the correct HTTP status codes are returned when dealing with non-existent pages?
PHP developers can ensure that the correct HTTP status codes are returned when dealing with non-existent pages by using the header function to set the appropriate status code, such as 404 Not Found. They can also include a custom error message or redirect the user to a designated error page.
<?php
// Check if page exists
if (!page_exists($page)) {
header("HTTP/1.0 404 Not Found");
echo "404 - Page not found";
// Or redirect to custom error page
// header("Location: error.php");
exit();
}
// Function to check if page exists
function page_exists($page) {
// Logic to check if page exists
return false;
}
?>