8

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

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.

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

And the third line exit terminates the script execution.

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

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

  • Mabye if anyone can explain why should i use the exit After the 404 header
  • Also why with the BOM i dont get "headers already sent error"

Thank you all and have a nice day.

4

2 回答 2

11

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

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

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

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

或直接

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();
}

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

于 2013-04-27T16:27:24.923 回答
1

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

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

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

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

于 2013-04-27T16:29:25.557 回答