Show 404 error page from PHP file, without redirecting

http-status-code-404, php

Solution

You can do it with headers.

header("HTTP/1.0 404 Not Found");

then you can add your 404 page using for example `readfile` method.

header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();

Problem

I have a file `secret.php` which is included in `index.php` and I want to show a 404 error page when someone tries to access `secret.php` directly. But ``` header("Location: this_site_certainly_does_not_exist"); ``` in `secure.php` is not a solution, because I want the URL the user typed (e.g. `example.com/secret.php`) to be visible in the browser's URL bar when the error page is shown, instead of redirecting.

Original source

Related problems