Why Does My PHP 404 Error Result in a Blank Page?

Mary-Kate Olsen
Release: 2024-11-13 14:53:02
Original
196 people have browsed it

Why Does My PHP 404 Error Result in a Blank Page?

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');
}
Copy after login

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:

  • User: "Do you have anything at this URI?"
  • Webserver: "No, 404! Here's a 404 error page."

However, with your PHP code:

  • User: "Do you have anything at this URI?"
  • Webserver: "Yes, it's a PHP page. It will tell you the status code."
  • PHP: "OMG 404!"
  • Webserver: "Oops, the error page people left, so I'll just send what PHP gave me."

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>';
Copy after login

The above is the detailed content of Why Does My PHP 404 Error Result in a Blank Page?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template