一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

header()设置浏览器的缓存与header语法

时间:2010-09-01 编辑:简简单单 来源:一聚教程网

定义和用法
header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php教程 4 以及更高的版本中,您可以使用输出缓存来解决此问题):

实例一

 代码如下 复制代码
// connect to the database:
$dbc = @mysql教程i_connect ('localhost', 'username', 'password', 'test') or die ('

could not connect to the database!

');
// get the latest dates as timestamps教程:
$q = 'select unix_timestamp(max(date_added)), unix_timestamp(max(date_completed)) from tasks';
$r = mysqli_query($dbc, $q);
list($max_a, $max_c) = mysqli_fetch_array($r, mysqli_num);
// determine the greater timestamp:
$max = ($max_a > $max_c) ? $max_a : $max_c;
// create a cache interval in seconds:
$interval = 60 * 60 * 6; // 6 hours
// send the header:
header ("last-modified: " . gmdate ('r', $max));
header ("expires: " . gmdate ("r", ($max + $interval)));
header ("cache-control: max-age=$interval");
?>

实例二

 代码如下 复制代码

// 结果出错
// 在调用 header() 之前已存在输出
header('location: http://www.111com.net/');
?>语法
header(string,replace,http_response_code)

提示用户保存一个生成的 pdf 文件(content-disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框):

 代码如下 复制代码

header("content-type:application/pdf");

// 文件将被称为 downloaded.pdf
header("content-disposition:attachment;filename='downloaded.pdf'");

// pdf 源在 original.pdf 中
readfile("original.pdf");
?>


热门栏目