php 发送头信息

 
更多

我们已经知道 PHP header可以送出status标头,如

<?php

header("http/1.1 404 not found"); 

?>

就可以让用户浏览器出现文件找不到的404错误,后来我查阅相关资料,终于试出来了如何header出状态代码(status),与大家分享。

其实应该是这样的:

第一部分为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



header('HTTP/1.1 404 Not Found');//常见的错误404页面

Header('http/1.1 403 Forbidden'); //403无权访问

header("HTTP/1.1 301 Moved Permanently");//301跳转

header("Location: http://www.baidu.com/");//跳转至所指向的链接

<?php
$domain = $_SERVER ['HTTP_HOST']; //获取当前域名

header("HTTP/1.1 301 Moved Permanently");

header("Location: http://www.".$domain);

?>
打赏

本文固定链接: https://www.cxy163.net/archives/337 | 绝缘体

该日志由 绝缘体.. 于 2012年04月02日 发表在 PHP 分类下,
原创文章转载请注明: php 发送头信息 | 绝缘体

报歉!评论已关闭.