首页 > 开发 > PHP > 正文

PHP中如何使用header发送头部信息

2024-05-04 22:57:28
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

在照彭武兴先生的《php bible》中所述,header可以送出status标头,如

header("status: 404 not found");

?>

就可以让用户浏览器出现文件找不到的404错误,但是我试了这样是不行的。

后来我到w3.org上查了http的相关资料,终于试出来了如何header出状态代码(status),与大家分享。

其实应该是这样的:

header("http/1.1 403 forbidden");

?>

第一部分为http协议的版本(http-version)

第二部分为状态代码(status)

第三部分为原因短语(reason-phrase)

三部分中间用一个空格分开,且中间不能有回车,第一部分和第二部分是必需的,第三部分则是给人看的,可写可不写甚至乱写。

还有,这一句的输出必须在html文件的第一行。

下面我给出各代码所代表的意思(是从w3.org上查到的,够权威了):

  * 1xx: informational - request received, continuing process

  * 2xx: success - the action was successfully received, understood,

  and accepted

  * 3xx: redirection - further action must be taken in order to

  complete the request

  * 4xx: client error - the request contains bad syntax or cannot be

  fulfilled

  * 5xx: server error - the server failed to fulfill an apparently

  valid request


       | "100" ; continue

       | "101" ; switching protocols

       | "200" ; ok

       | "201" ; created

       | "202" ; accepted

       | "203" ; non-authoritative information

       | "204" ; no content

       | "205" ; reset content

       | "206" ; partial content

       | "300" ; multiple choices

       | "301" ; moved permanently

       | "302" ; moved temporarily

       | "303" ; see other

       | "304" ; not modified

       | "305" ; use proxy

       | "400" ; bad request

       | "401" ; unauthorized

       | "402" ; payment required

       | "403" ; forbidden

       | "404" ; not found

       | "405" ; method not allowed

       | "406" ; not acceptable

       | "407" ; proxy authentication required

       | "408" ; request time-out

       | "409" ; conflict

       | "410" ; gone

       | "411" ; length required

       | "412" ; precondition failed

       | "413" ; request entity too large

       | "414" ; request-uri too large

       | "415" ; unsupported media type

       | "500" ; internal server error

       | "501" ; not implemented

       | "502" ; bad gateway

       | "503" ; service unavailable

       | "504" ; gateway time-out

       | "505" ; http version not supported

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表