教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP中header()函数有什么用?常见header 状态

PHP中header()函数有什么用?常见header 状态

发布时间:2016-10-18   编辑:jiaochengji.com
教程集为您提供PHP中header()函数有什么用?常见header 状态等资源,欢迎您收藏本站,我们将为您提供最新的PHP中header()函数有什么用?常见header 状态资源
在php中header()函数是很大的作用可以发送各种状态代码,也可以实现一些输出下载,下面我们一起来看看一些相关实例吧。

什么是头信息?
这里只作简单解释,详细的自己看http协议。
在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(header) 和 体内容,这里的头信息不是HTML中的<head></head>部分,同样,体内容也不是<BODY>< /BODY>。头信息是用户看不见的,里面包含了很多项,包括:服务器信息、日期、内容的长度等。而体内容就是整个HTML,也就是你所能看见的全 部东西。

头信息有什么用呢?
头信息的作用很多,最主要的有下面几个:

1、跳转:当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址,这点有点类似用 js 写跳转。但是这个跳转只有浏览器知道,不管体内容里有没有东西,用户都看不到。

2、指定网页的内容: 同样一个XML文件,如果头信息中指定:Content-type: application/xml 的话,浏览器会将其按照XML文件格式解析。但是,如果头信息中是:Content-type: text/xml 的话,浏览器就会将其看作存文本解析。(浏览器不是按照扩展名解析文件的)

3、附件:不知道大家有没 有注意,有些时候在一些网站下载东西,点下载连接以后,结果浏览器将这个附件当成网页打开了,里面显示的都是乱码,这个问题也和头信息有关。有时候浏览器 根据Content-type 来判断是打开还是保存,这样有时就会判断错误(主要是网站设计者忘记写Content-type)。其实,还有一个可以来指定该内容为附件、需要保存,这 个就是:Content-Disposition: attachment; filename=”xxxxx”

在PHP中如何写?
1、跳转:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8118')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8118>header(“Location: http://www.example.com/”);

2、指定内容:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4173')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4173>header(‘Content-type: application/pdf’);

3、附件:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6360')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6360>header(‘Content-type: application/pdf’); // 指定内容格式
header(‘Content-Disposition: attachment; filename=”downloaded.pdf”‘); // 指定内容为附件
readfile(‘original.pdf’); // 打开文件,并输出

最后要提醒大家注意一点,所有头信息都必须在体内容之前,如果一旦有任何输出了的话,header函数写的头信息就没用了。比如,在文件最开头 的<?php 处,如果前面有空格或者有空行,那header函数就没用了(其实可以通过设置:output_buffer来解决,anyway),为什么这样,可以看 看HTTP协议,很简单。

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2109')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2109><?php
//200 正常状态
header('HTTP/1.1 200 OK');
 
// 301 永久重定向,记得在后面要加重定向地址 Location:$url
header('HTTP/1.1 301 Moved Permanently');
 
// 重定向,其实就是302 暂时重定向
header('Location: http://www.jiaochengji.com/');
 
// 设置页面304 没有修改
header('HTTP/1.1 304 Not Modified');
 
// 显示登录框,
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="登录信息"');
echo '显示的信息!';
 
// 403 禁止访问
header('HTTP/1.1 403 Forbidden');
 
// 404 错误
header('HTTP/1.1 404 Not Found');
 
// 500 服务器错误
header('HTTP/1.1 500 Internal Server Error');
 
// 3秒后重定向指定地址(也就是刷新到新页面与 <meta http-equiv="refresh" content="10;http://www.jiaochengji.com/ /> 相同)
header('Refresh: 3; url=http://www.jiaochengji.com/');
echo '10后跳转到http://www.jiaochengji.com';
 
// 重写 X-Powered-By 值
header('X-Powered-By: PHP/5.3.0');
header('X-Powered-By: Brain/0.6b');
 
//设置上下文语言
header('Content-language: en');
 
// 设置页面最后修改时间(多用于防缓存)
$time = time() - 60; //建议使用filetime函数来设置页面缓存时间
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
 
// 设置内容长度
header('Content-Length: 39344');
 
// 设置头文件类型,可以用于流文件或者文件下载
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
readfile('example.zip');//读取文件到客户端
 
//禁用页面缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');
 
//设置页面头信息
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain');
header('Content-Type: image/jpeg');
header('Content-Type: application/zip');
header('Content-Type: application/pdf');
header('Content-Type: audio/mpeg');
header('Content-Type: application/x-shockwave-flash');
//.... 至于Content-Type 的值 可以去查查 w3c 的文档库,那里很丰富
?>

例,文件下载

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3400')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3400>

header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".$title .".xls ");
header("Content-Transfer-Encoding: binary ");

您可能感兴趣的文章:
PHP中header()函数有什么用?常见header 状态
php header的作用
php header 使用详解
php中header函数的用法举例详解
PHP header函数用法举例
PHP header()函数(301、404等错误设置)的用法
PHP header()函数使用详解
php header是什么意思
php header函数用法详解举例(2)
php header头信息应用举例

[关闭]
~ ~