调整浏览器缓存

php的header()函数可以调整web浏览器和代理服务器的缓存。这里涉及4种头标类型。

  • Last-Modified(最后修改时间)
  • Expires(有效期限)
  • Pragma(编译提示)
  • Cache-Control(缓存控制)

Pragma声明了页面数据应该如何被处理。可以这样避免页面缓存:

            header("Pragma:no-cache");

Cache-Control头标是在http1.1里面添加的

指令含义
public可以在任何地方缓存
private只能被浏览器缓存
no-cache不能在如何地方缓存
must-revalidate缓存必须检查更新版本
proxy-revalidate代理缓存必须检查更新版本
max-age内容能够被缓存的时期,以秒表示
s-maxage覆盖共享缓存的max-age

把这些信息综合在一起,为了避免如何系统对页面进行缓存,应该使用如下头标

header("Last-Modified:Thu, 29 Oct 2007 14:26:00 GMT"); // Right Now

header("Expires:Mon, 29 Oct 1976 05:00:00 GMT");

header("Pragma:no-cache");

header("Cache-Control:no-cache");


<?php
	$dbc = mysqli_connect("localhost","root","gonglishan520","xsphpdb") or die('<h1>could not connect to database</h1>');
	/*
	*使用unix_timestamp函数把返回值转换为整数
	*
	*/
	$q = 'select unix_timestamp(max(date_added)),unix_timestamp(max(date_completed)) from tasks';
	$r = mysqli_query($dbc,$q);
	list($max_a, $max_c) = mysql_fetch_array($dbc,$r);
	
	$max = ($max_a > $max_c) ? $max_a : $max_c;
	
	$interval = 60 * 60 * 6;
	/*
	*gmdate(),date()函数只要设置了第一个参数为为'r'
	*就会根据http规范返回相应的日期格式
	*/
	header("Last-Modified:".gmdate('r',$max));
	header("Expires:".gmdate('r',$max + $interval));
	header("Cache-Control:max-age:$interval");		
?>
<html>
	<head></head>
	<body>
		<h3>current to-do list</h3>        
	</body>
</html>
<?php
	function make_list($parent){
		global $tasks;
		
		echo '<ol>';
		foreach($parent as $task_id => $todo){
			if(isset($tasks[$task_id])){
				make_list($tasks[$task_id]);	
			}
		}
		echo '</ol>';
	}
	
	$q = 'select task_id,parent_id,task from tasks where date_completed="0000-00-00 00:00:00" order by parent_id, date_added asc';
	
	$r = mysqli_query($dbc,$q);
	
	$tasks = array();
	
	while(list($task_id,$parent_id,$task) = mysqli_fetch_array($r,MYSQLI_NUM)){
		$tasks[$parent_id][$task_id] = $task;	
	}
	
	make_list($tasks[0]);
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值