php怎么让代码出错时返回错误代码

本篇内容介绍了“php怎么让代码出错时返回错误代码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

本文操作环境: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怎么让代码出错时返回错误代码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!