为什么我的 PHP 应用程序不会发送 404 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/437256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't my PHP app send a 404 error?
提问by Alan Storm
if (strstr($_SERVER['REQUEST_URI'],'index.php')) {
header('HTTP/1.0 404 Not Found');
}
Why wont this work? I get a blank page.
为什么这行不通?我得到一个空白页。
回答by Alan Storm
Your code is technically correct. If you looked at the headers of that blank page, you'd see a 404 header, and other computers/programs would be able to correctly identify the response as file not found.
您的代码在技术上是正确的。如果您查看该空白页面的标题,您会看到一个 404 标题,其他计算机/程序将能够正确地将响应识别为未找到文件。
Of course, your users are still SOL. Normally, 404s are handled by the web server.
当然,您的用户仍然是 SOL。通常,404 由 Web 服务器处理。
- User: Hey, do you have anything for me at this URI webserver?
- Webserver: No, I don't, 404! Here's a page to display for 404s.
- 用户:嘿,你在这个 URI 网络服务器上有什么要给我的吗?
- 网络服务器:不,我不知道,404!这是一个显示 404 的页面。
The problem is, once the web server starts processing the PHP page, it's already passed the point where it would handle a 404
问题是,一旦 Web 服务器开始处理 PHP 页面,它就已经超过了处理 404 的点
- User: Hey, do you have anything for me at this URI webserver?
- Webserver: Yes, I do, it's a PHP page. It'll tell you what the response code is
- PHP: Hey, OMG 404!!!!!!!
- Webserver: Well crap, the 404 page people have already gone home, so I'll just send along whatever PHP gave me
- 用户:嘿,你在这个 URI 网络服务器上有什么要给我的吗?
- 网络服务器:是的,我知道,它是一个 PHP 页面。它会告诉你响应代码是什么
- PHP:嘿,我的天哪 404 !!!!!!!!!
- 网络服务器:废话,404 页面的人已经回家了,所以我只会发送 PHP 给我的任何内容
In addition to providing a 404 header, PHP is now responsible for outputting the actual 404 page.
除了提供 404 标头之外,PHP 现在还负责输出实际的 404 页面。
回答by Evan Fosmark
if (strstr($_SERVER['REQUEST_URI'],'index.php')){
header('HTTP/1.0 404 Not Found');
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
}
If you look at the last two echo lines, that's where you'll see the content. You can customize it however you want.
如果您查看最后两条 echo 行,您就会看到内容。您可以根据需要自定义它。
回答by Bob Fanger
That is correct behaviour, it's up to you to create the contents for the 404 page.
The 404 header is used by spiders and download-managers to determine if the file exists.
(A page with a 404 header won't be indexed by google or other search-engines)
这是正确的行为,由您来创建 404 页面的内容。
蜘蛛和下载管理器使用 404 标头来确定文件是否存在。
(带有 404 标题的页面不会被谷歌或其他搜索引擎索引)
Normal users however don't look at http-headers and use the page as a normal page.
但是,普通用户不会查看 http 标头并将该页面用作普通页面。
回答by Silviu-Marian
For the record, this is the all-case handler:
作为记录,这是所有案例处理程序:
<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
header("Status: 404 Not Found");
$_SERVER['REDIRECT_STATUS'] = 404;
?> <!-- 404 contents below this line -->
回答by Kitet
Load default server 404 page, if you have one, e.g. defined for apache:
加载默认服务器 404 页面,如果你有一个,例如为 apache 定义的:
if(strstr($_SERVER['REQUEST_URI'],'index.php')){
header('HTTP/1.0 404 Not Found');
readfile('404missing.html');
exit();
}
回答by rparker
Since php 5.4 you can now do http_response_code(404);
从 php 5.4 你现在可以做 http_response_code(404);
回答by Olaf
try with:
尝试:
header("Status: 404 Not Found");
header('HTTP/1.0 404 Not Found');
Bye!
再见!
回答by Earth Engine
Another solution, based on @Kitet's.
另一个解决方案,基于@Kitet's。
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
header("Status: 404 Not Found");
$_SERVER['REDIRECT_STATUS'] = 404;
//If you don't know which web page is in use, use any page that doesn't exists
$handle = curl_init('http://'. $_SERVER["HTTP_HOST"] .'/404missing.html');
curl_exec($handle);
If you are programming a website that hosted in a server you do not have control, you will not know which file is the "404missing.html". However you can still do this.
如果您正在编写一个托管在您无法控制的服务器中的网站,您将不知道哪个文件是“404missing.html”。但是,您仍然可以这样做。
In this way, you provided exactly the same outcome of a normal 404 page on the same server. An observer will not be able to distinguish between an existing PHP page returns 404 and a non-existing page.
通过这种方式,您在同一台服务器上提供了与普通 404 页面完全相同的结果。观察者将无法区分现有的 PHP 页面返回 404 和不存在的页面。
回答by Jesse
if($_SERVER['PHP_SELF'] == '/index.php'){
header('HTTP/1.0 404 Not Found');
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
die;
}
never simplify the echo statements, and never forget the semi colon like above, also why run a substr on the page, we can easily just run php_self
永远不要简化 echo 语句,永远不要忘记上面的分号,还有为什么在页面上运行 substr,我们可以轻松地运行 php_self
回答by Jesse
A little bit shorter version. Suppress odd echo.
短一点的版本。抑制奇怪的回声。
if (strstr($_SERVER['REQUEST_URI'],'index.php')){
header('HTTP/1.0 404 Not Found');
exit("<h1>404 Not Found</h1>\nThe page that you have requested could not be found.");
}

