PHP: prevent direct access to page

http-status-code-404, php

Solution

Don’t redirect but send the 404 status code:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;

Problem

I have some pages that I don't want users to be able to access directly. I have this function I came up with which works: ``` function prevent_direct_access() { if($_SERVER['REQUEST_URI'] == $_SERVER['PHP_SELF']) { //include_once('404.php'); header("Location: 404.php"); } } ``` This does exactly what I want, the URL does not change but the content does. However I am wondering if there is something I need to add to tell search engines that this is a 404 and not to index it. keep in mind I do not want the URL to change though. Thanks!

Original source