Why won't my PHP app send a 404 error?
You've written the logic to detect index.php in the request URI and tried to force a 404 Not Found response. The code looks syntactically correct, yet the result is a blank screen instead of the expected HTTP status code. This behavior isn't a mystery; it's a direct consequence of how PHP handles output buffering and header manipulation relative to the document root.
The snippet you provided checks $_SERVER['REQUEST_URI'] for the string index.php. While this logic identifies files within your application directory, the execution flow is where things break down. In many PHP configurations, especially when running on web servers like Apache or Nginx with specific setups, the server might serve index.php as a standalone entry point before your script even initializes. If the file index.php exists in the document root and is being accessed directly, the server treats it as a valid resource unless explicitly told otherwise via a configuration directive that intercepts the request before PHP generates output.
The critical issue here is timing. The header() function must be called before any output is sent to the browser. In your case, if the script containing that if statement is actually index.php itself, or if there is a default index.php being served by the server's auto-indexing or routing logic before your code runs, you might inadvertently trigger an output handler. However, the more common culprit for a "blank page" when attempting to send headers in this specific pattern is that the script executing the header check is not the one actually generating the response body, or the web server has already committed to serving content before your PHP process can intervene with a raw 404.
Consider the scenario where your application expects index.php to be the entry point, but you are trying to block access to it specifically via a header check inside that same file. If the request hits index.php, and that file immediately checks its own name in the URI, it enters the if block. It then calls header('HTTP/1.0 404 Not Found'). At this exact moment, if the server configuration or a preceding include/statement has flushed any buffersâeven an empty lineâthe header will fail silently. PHP will not throw an error by default; it will simply ignore the failed header call and proceed to render whatever comes next. If there is no subsequent code that generates HTML, the browser receives a 404 status but displays nothing because there is no body content associated with that status in your script.
To fix this, you need to ensure the header is sent before any output, including whitespace or empty lines, and verify that the server supports raw status codes for that specific URI. A more robust approach often involves checking if headers have already been sent using headers_sent() before attempting to modify them:
if (strstr($_SERVER['REQUEST_URI'], 'index.php') && !headers_sent()) {
http_response_code(404); // Prefer http_response_code for modern PHP versions
exit;
}
Using http_response_code() is generally preferred over header('HTTP/...') in PHP 5.6 and later because it sets the status code regardless of whether a header was previously sent, though you must still ensure no output has occurred. The exit statement is crucial here; without it, the script continues running, potentially executing logic that generates content, which would corrupt the 404 response or cause the header to be overridden.
Another consideration is your server's .htaccess file or Nginx configuration. If you are trying to achieve a global 404 for index.php, it might be cleaner to handle this at the web server level rather than in application logic. For example, an Apache rewrite rule can redirect requests for index.php directly to a custom error page with the correct status code without relying on PHP output buffering nuances.
If your goal is to protect sensitive routes or specific files from being accessed directly, relying solely on a header check inside the file is fragile. It depends entirely on the execution order and the absence of accidental whitespace. For production environments, combining application-level checks with server-level configuration ensures that the 404 status is returned correctly even if output buffering settings vary across different hosting providers.