PHP 函数:header()

header

送出 HTTP 协议的标头到浏览器

语法: int header(string string);

返回值: 整数

函数种类: 网络系统

内容说明

标头 (header) 是服务器以 HTTP 协议传 HTML 资料到浏览器前所送出的字符串,在标头与 HTML 文件之间尚需空一行分隔。有关 HTTP 的详细说明,可以参考坊间的相关书籍或更详细的 RFC 2068 官方文件([url]http://www.w3.org/Protocols/rfc2068/rfc2068[/url])。在 PHP 中送回 HTML 资料前,需先传完所有的标头。

注意: 传统的标头一定包含下面三种标头之一,并只能出现一次。

Content-Type: xxxx/yyyy
Location: xxxx:yyyy/zzzz
Status: nnn xxxxxx
在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。

使用范例
范例一: 本例用来重导用户到 PHP 的官方网站。

<?php Header("Location: [url]http://www.php.net[/url]"); exit; ?>

范例二: 欲让用户每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header("Last-Modified: " . gmdate(“D, d M Y H:i:s”) . “GMT”);
header(“Cache-Control: no-cache, must-revalidate”);
header(“Pragma: no-cache”);

范例三: 让用户的浏览器出现找不到文件的信息。

<?php header("Status: 404 Not Found"); ?>

范例四: [email protected] (28-Apr-1999) 提供让用户下载文件的范例。
header(“Content-type: application/x-gzip”);
header(“Content-Disposition: attachment; filename=some-file.tar.gz”);
header(“Content-Description: PHP3 Generated Data”);