指令 | 含义 |
public | 可以在任何地方缓存 |
private | 只能被浏览器缓存 |
no-cache | 不能在任何地方缓存 |
must-revalidate | 缓存必须检查更新版本 |
proxy-revalidate | 代理缓存必须检查更新版本 |
max-age | 内容能够被缓存的时期,以秒表示 |
s-maxage | 覆盖共享缓存的max-age设置 |
下面实例利用header()设置浏览器的缓存:
<?php # Script 2.7 - view_tasks.php // Connect to the database: $dbc = @mysqli_connect ('localhost', 'username', 'password', 'test') OR die ('<p>Could not connect to the database!</p></body></html>'); // 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"); ?>
<?php $interval=60*60*6 ; ?>
<?php header("Last-Modified:".gmdate("r",($max+$interval))); ?>
<?php header ("Expires: " . gmdate ("r", ($max + $interval))); ?>
<?php header ("Cache-Control: max-age=$interval"); ?>
getallheaders()函数:
<?php foreach (getallheaders() as $name => $value) { echo "$name: $value\n"."<br />"; } ?>输出结果:
Host: 127.0.0.1 Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36 Referer: http://127.0.0.1/liu/ Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8
下面组合是禁用缓存
<?php 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"); ?>