PHP 404 Error Evasion: Unmasking the Hidden Communication Flaw
When attempting to send a 404 error with PHP, you may encounter an unexpected outcome: a blank page. To understand why this occurs, let's investigate the code you provided:
if (strstr($_SERVER['REQUEST_URI'],'index.php')) { header('HTTP/1.0 404 Not Found'); }
While technically correct, this code only sets the HTTP header to "404 Not Found." However, the web server has already begun processing the PHP page before this header can be intercepted.
The normal flow of a 404 error looks like this:
However, with your PHP code:
Therefore, despite the 404 header, the browser still receives the PHP output and renders a blank page. To resolve this issue, PHP must also provide the 404 error page. For instance, you could use the following code:
header('HTTP/1.0 404 Not Found'); echo '<html><head><title>404 Not Found</title></head><body><h1>404 Error</h1></body></html>';
以上是為什麼我的 PHP 404 錯誤會導致空白頁?的詳細內容。更多資訊請關注PHP中文網其他相關文章!