为什么我的 PHP 404 错误会导致空白页?

Mary-Kate Olsen
发布: 2024-11-13 14:53:02
原创
173 人浏览过

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');
}
登录后复制

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>';
登录后复制

以上是为什么我的 PHP 404 错误会导致空白页?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板