1. 通过header()方法来实现,

最简单的方法,而且对php的版本没什么限制

   <?php

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

         header("status: 404 Not Found");

   ?>

当服务器设置了404页面后,访问该php就会自动返回404状态及404页面。

2. 使用http_response_code()方法,

     如果你的php版本大于5.4的话,可以使用这个新增的方法

  <?php

        http_response_code(404);

    ?>

    注意:如果你的版本低于5.4,但是你又非得使用http_response_code(),下面是这个方法的代码

 <?php

     if (!function_exists('http_response_code')) {
function http_response_code($code = NULL) { if ($code !== NULL) { switch ($code) {
case 100: $text = 'Continue'; break;
case 101: $text = 'Switching Protocols'; break;
case 200: $text = 'OK'; break;
case 201: $text = 'Created'; break;
case 202: $text = 'Accepted'; break;
case 203: $text = 'Non-Authoritative Information'; break;
case 204: $text = 'No Content'; break;
case 205: $text = 'Reset Content'; break;
case 206: $text = 'Partial Content'; break;
case 300: $text = 'Multiple Choices'; break;
case 301: $text = 'Moved Permanently'; break;
case 302: $text = 'Moved Temporarily'; break;
case 303: $text = 'See Other'; break;
case 304: $text = 'Not Modified'; break;
case 305: $text = 'Use Proxy'; break;
case 400: $text = 'Bad Request'; break;
case 401: $text = 'Unauthorized'; break;
case 402: $text = 'Payment Required'; break;
case 403: $text = 'Forbidden'; break;
case 404: $text = 'Not Found'; break;
case 405: $text = 'Method Not Allowed'; break;
case 406: $text = 'Not Acceptable'; break;
case 407: $text = 'Proxy Authentication Required'; break;
case 408: $text = 'Request Time-out'; break;
case 409: $text = 'Conflict'; break;
case 410: $text = 'Gone'; break;
case 411: $text = 'Length Required'; break;
case 412: $text = 'Precondition Failed'; break;
case 413: $text = 'Request Entity Too Large'; break;
case 414: $text = 'Request-URI Too Large'; break;
case 415: $text = 'Unsupported Media Type'; break;
case 500: $text = 'Internal Server Error'; break;
case 501: $text = 'Not Implemented'; break;
case 502: $text = 'Bad Gateway'; break;
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
exit('Unknown http status code "' . htmlentities($code) . '"');
break;
} $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); header($protocol . ' ' . $code . ' ' . $text); $GLOBALS['http_response_code'] = $code; } else { $code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); } return $code; }
} ?>

PHP返回404状态码,由服务器处理的更多相关文章

  1. Nginx自定义404页面并返回404状态码

    Nginx定义404页面并返回404状态码, WebServer是nginx,直接告诉我应该他们配置了nginx的404错误页面,虽然请求不存在的资源可以成功返回404页面,但返回状态码确是200. ...

  2. PHP发送返回404状态码

    1. 默认的由Apache自动处理的404 修改Aache的配置文件 httpd.conf 中的 ErrorDocument 404 /404.html 或者使用 .htaccess文件,同时有要把 ...

  3. <aop:aspectj-autoproxy proxy-target-class="false"/>导致出现404状态码

    今天干活的时候,由于是一个web应用,想在每次发送请求和返回响应的时候记录日志,也就是代理Controller,想起了之前的spring AOP,于是按照之前的配置配置好了,可是发现每次前端发送请求都 ...

  4. IIS7.0设置404错误页,返回500状态码

    一般在II6下,设置自定义404错误页时,只需要在错误页中选择自定义的页面,做自己的404页面即可.但是在IIS7.0及以上时,设置完404错误页后,会发现状态码返回的是500,并且可能会引起页面乱码 ...

  5. iis设置404错误页,返回500状态码

    一般在II6下,设置自定义404错误页时,只需要在错误页中选择自定义的页面,做自己的404页面即可.但是在IIS7.0及以上时,设置完404错误页后,会发现状态码返回的是500,并且可能会引起页面乱码 ...

  6. ASP.NET设置404页面返回302HTTP状态码的解决方法

    在配置文件中配置404页面如下: .代码如下: <customErrors mode="On" defaultRedirect="404.aspx"> ...

  7. ASP.NET自定义错误页并返回正确的500、404状态码

    在项目中,我们常常需要自定义错误页面,但往往返回的状态码都变成了200,对SEO很不友好.我尝试过在百度上寻找解决方案,但找到的资料中说的方法都试过了,发现都是无法返回正确的状态码的. 最后,只好自已 ...

  8. iis站点设置错误页面返回http状态码为404而不是302或其他

    今天一位客户说网站错误页面返回的状态码是302而不是404,问ytkah要如何处理.这个应该是设置没有正确的原因.我们一步步来排查一下.1.首先打开iis管理器,左侧选择具体的站点,在右侧窗口中点击4 ...

  9. python(30) 获取网页返回的状态码,状态码对应问题查询

    获取访问网页返回的状态码 html = requests.get(Url) respon = html.status_code 以下内容来自于维基百科:点击查看网页 1xx消息 这一类型的状态码,代表 ...

随机推荐

  1. C# 调用带输入输出参数的存储过程

    //调用存储过程执行类似于2//select count(*) from userinfo where username=username and pwd=pwd and grade=grade3// ...

  2. xml构建

    <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=346252320&site=qq ...

  3. mysql用户增删改

    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 1.1 登录MYSQL: @>mysql -u root -p @&g ...

  4. Windows10使用

    1.常见问题 Win10 的操作中心如果不见了 Windows10电脑系统时间校准 实现windows与ubuntu的之间的复制与粘贴 安卓手机传递文件到Windows系统电脑 安卓手机高速传递文件到 ...

  5. “全栈2019”Java第三十四章:可变参数列表

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. LAMPer 技能树

  7. ArchLinux下Ecplise软件报错

    如果你的Java出现了一下问题:An error has occurred. See error log for more details.java.lang.NullPointerException ...

  8. jmeter简单的接口请求

    骤是:在测试计划下面新建一个线程组,线程组下面添加请求,再线程下加入HTTP信息头管理器(如果要求传此参数的话)请求中添加结果和断言 1.打开页面添加一个线程组: 2.线程组中设置参数: 很重要的几个 ...

  9. ubuntu 安装python3

    1.安装python3 apt-get install python3 2.安装pip3 apt-get install python3-pip 3.为python3添加包 pip3 install ...

  10. 微信朋友圈评论/回复/cell/键盘谈起

    微信朋友圈评论功能的细节考虑及实现       微信朋友圈回复tableview iOS 实现微信朋友圈评论回复功能(一)