PHP 404 未找到标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16254291/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:47:00  来源:igfitidea点击:

Php 404 Not Found Header

phpheader

提问by Aviel Fedida

I am trying to understand the combination of 3 simple php code lines, This is the code:

我试图理解 3 条简单的 php 代码行的组合,这是代码:

ob_end_clean();
header('HTTP/1.0 404 Not Found');
exit;

So this is the code and as i understand the first line ob_end_clean();, Can help for example with BOM(Byte order mark), So the first line is to prevent any previous output.

所以这是代码,正如我所理解的第一行ob_end_clean(); , 可以帮助例如BOM(Byte order mark),所以第一行是防止任何以前的输出。

The second line header('HTTP/1.0 404 Not Found');is the header.

第二行header('HTTP/1.0 404 Not Found'); 是标题。

And the third line exitterminates the script execution.

第三行exit终止脚本执行。

If i remove the first line and i got a BOMat the document i get blank page (No 404).

如果我删除第一行并且我在文档中得到了一个BOM,我会得到空白页(没有 404)。

If i remove the third line (with and without the BOM), I get the page i wanted no blank page and no 404.

如果我删除第三行(有和没有 BOM),我会得到我想要的页面,没有空白页,也没有 404。

  • Mabye if anyone can explain why should i use the exitAfter the 404 header
  • Also why with the BOMi dont get "headers already sent error"
  • Mabye 如果有人可以解释为什么我应该使用exitAfter 404 标头
  • 还有为什么使用BOM我没有收到“标题已发送错误”

Thank you all and have a nice day.

谢谢大家,祝你有美好的一天。

回答by Lukas Greso

If i remove the first line and i got a BOM at the document i get blank page (No 404). you get blank 404 because you have no content defined in there...

如果我删除第一行并且我在文档中得到了一个 BOM,我会得到空白页(没有 404)。你得到空白 404 因为你没有在那里定义内容......

header('HTTP/1.0 404 Not Found');

is only giving notice that user is on 404 error page site... if you want to display some 404 notice for user you can do this by loading your 404.html file

只是通知用户在 404 错误页面站点上...如果您想为用户显示一些 404 通知,您可以通过加载 404.html 文件来执行此操作

if(strstr($_SERVER['REQUEST_URI'],'index.php')){
  header('HTTP/1.0 404 Not Found');
  readfile('404missing.html');
  exit();
}

or directly

或直接

if (strstr($_SERVER['REQUEST_URI'],'index.php')){
    header('HTTP/1.0 404 Not Found');
    echo "<h1>Error 404 Not Found</h1>";
    echo "The page that you have requested could not be found.";
    exit();
}

exit function is there because you have to prevent execution of another php code, which may be directly after ifor which may be excecuted later, simply it says END

退出函数的存在是因为你必须阻止另一个 php 代码的执行,它可能直接在之后if或可能在稍后执行,简单地说END

回答by Lightness Races in Orbit

why should i use the exit After the 404 header

为什么我应该在 404 标头之后使用 exit

So that no further code will be executed. If there isn't any then, fine, it isn't necessary in this case. It's a good habit to get into though.

这样就不会执行进一步的代码。如果没有,那很好,在这种情况下没有必要。进入虽然是一个好习惯。

Also why with the BOM i dont get "headers already sent error"

还有为什么使用 BOM 我没有收到“标题已发送错误”

You didn't configure your PHP installation to show errors and notices to the end user.

您没有将 PHP 安装配置为向最终用户显示错误和通知。