php的header函数返回404失败

发布于 2021-11-25 15:06:06 字数 1316 浏览 917 评论 7

刚学习php,学到header的时候试着使用

header("http/1.1 404 Not Found");

但是运行之后没有反应,网上说这句话应该放在文件开头,整个文件如下:

<?php
header("http/1.1 404 Not Found");
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        echo "hello";
        ?>
    </body>
</html>



但是运行之后查看网页代码如下:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        hello    </body>
</html>



header函数没有起到作用。

现在想知道这个需要apache或者是php额外配置什么?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

兮颜 2021-11-26 05:19:01

用浏览器开发者工具查看请求返回的状态。

瑾兮 2021-11-26 05:15:27

两者不冲突

404 状态 后边 也可以继续写入内容

很多网站 也都是 自定义的 404页面

header之后调用exit(); 也就真没有后续的内容了。。

够钟 2021-11-26 05:12:18

明白了…

秉烛思 2021-11-26 05:09:41

header之后调用exit();

鹤舞 2021-11-26 04:08:08

我在netbeans的网络监视器中发现了: Request URL: http://localhost/PhpProject1/index.php Method: GET Status: 404 倒是找到了404的代码,但是为什么还是显示了下面的网页呢,不是应该直接显示网页未找到么。

你曾走过我的故事 2021-11-26 04:07:49

回复
那是IE专用。。。现在都是返回404同时显示一个引导页面

霞映澄塘 2021-11-25 16:16:31

代码没问题,注意规范header('HTTP/1.1 404 Not Found'); 

打开Chrome的控制台查看

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文