Home >Backend Development >PHP Tutorial >Why Does My PHP App Show a Blank Page Instead of a 404 Error?
PHP 404 Error: Understanding the Last Mile Gap
Despite following the seemingly correct code, why does your PHP application fail to display a 404 error and instead presents a blank page?
The provided code ensures that a 404 error header is sent. However, it fails to account for a crucial distinction in how responses are handled by web servers versus PHP.
Web Server Handling
Typically, the web server processes the request and, if it cannot find the file requested at the URI, it sends a 404 header and displays a custom 404 error page.
PHP Interception
When the web server delegates the request to the PHP page, thePHP script is responsible for generating both the header and the response body. This means that despite the 404 header being sent by PHP, the web server has already committed to displaying the PHP page instead of the standard 404 page.
Handing Over to PHP
As a result, the PHP script effectively takes over the responsibility of handling the 404 error, which presents two challenges:
Solution
To resolve the issue, you must explicitly handle the 404 case and output an error message yourself. The following code provides an example:
if (strstr($_SERVER['REQUEST_URI'],'index.php')) { header('HTTP/1.0 404 Not Found'); echo '<h1>404 Not Found</h1>'; exit; }
The above is the detailed content of Why Does My PHP App Show a Blank Page Instead of a 404 Error?. For more information, please follow other related articles on the PHP Chinese website!