• 技术文章 >后端开发 >PHP问题

    php怎么返回错误代码

    藏色散人藏色散人2021-09-11 09:16:32原创136

    在PHP中可以使用header()函数返回错误代码,也就是给页面添加404的头信息,其语法如“header("HTTP/1.0 404 Not Found");”。

    本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

    php怎么返回错误代码?

    php中用header()函数是可以为返回页面添加404的头信息的,从而提示浏览器该网页找不到了。

    所以可以使用:

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

    或者:

    header("Status: 404 Not Found");

    后者是在FastCGI模式下使用的,在php代码中可以把两句直接同时写上。

    摘抄php手册官网的header使用说明如下:

    The header string.There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.For FastCGI you must use the following for a 404 response:

    而header("Location: xxx.com");默认是做302状态的跳转,所以它是不能给浏览器输出404错误状态的。

    推荐学习:《PHP视频教程

    以上就是php怎么返回错误代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php
    上一篇:php怎么查询mysql数据显示 下一篇:如何解决php连接mysql 500错误问题
    线上培训班

    相关文章推荐

    • php怎么将错误日志写入文件• php curl返回错误码60怎么办• php curl怎么输出错误• php怎么进行错误和异常处理

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网