代码之家  ›  专栏  ›  技术社区  ›  Greg

PHP 404响应头

  •  0
  • Greg  · 技术社区  · 14 年前

    我试图实现这一目标的方法是:

    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
    

    HTTP/1.1 404 Not Found 
    

    但是,我希望能够输出一个友好的404页,但是header()函数之后的任何内容都会被忽略,之前的任何内容都会被忽略。。。好。。。那太蠢了。

    谢谢。

    3 回复  |  直到 14 年前
        1
  •  3
  •   ontrack    14 年前

    您必须确保输出(或ErrorDocument)大于512字节,以确保internetexplorer不显示自己的错误页。这可能就是为什么在标题没有显示之后您会遇到任何输出。

        2
  •  1
  •   ololo    14 年前
    <?php
    header("HTTP/1.0 404 Not Found");
        echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
    <html><head>
    <title>404 Not Found</title>
    </head><body>
    <h1>Not Found</h1>
    <p>The requested URL " . $_SERVER['REQUEST_URI'] . " was not found on this server.</p>
    <hr>
    <address>Apache/2.2.3 (CentOS) Server at " . $_SERVER['SERVER_NAME'] . " Port 80</address>
    </body></html>";
        exit();
    ?>
    
        3
  •  0
  •   Michal Drozd    14 年前

    ErrorDocument 404 /errordoc.html
    

    不是为你工作吗?

    编辑: 如您所见,您可以轻松地在PHP中抛出404,然后在.htaccess中捕获它。