Home  >  Article  >  Backend Development  >  Code to set browser cache using header() function under PHP_PHP tutorial

Code to set browser cache using header() function under PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:19840browse

This involves 4 header types:

Last-Modified (last modification time);
Expires (validity period);
Pragma (compilation instructions);
Cache-Control (cache Control);
The first three headers belong to the HTTP1.0 standard. The Last-Modified header uses UTC date and time values. If the caching system sees that the Last-Modified value is closer to the current time than the cached version of the page, it knows it should use the new version from the server.

Expires indicates when the cached version should expire (GMT). Setting this to a previous time will force the page on the server to be used.

Pragma defines how the page data should be processed. You can avoid caching the page like this:

header("Pragma:no-cache");

The Cache-Co0ntrol header was added in HTTP1.1, which can achieve more detailed Control (should also continue to use HTTP 1.0 headers). There are
many settings for Cache-Control, as shown in the following table:

The following example uses header() to set the browser's cache:

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

Copy the code The code is as follows:
// Connect to the database:
$dbc = @mysqli_connect ('localhost', 'username', 'password', 'test') OR die ('');
// 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");
?>

1. After connecting to the database, obtain the latest date values ​​date_added and date_completed in the data table, use the UNIX_TIMESTAMP() function to convert the return value into an integer and then obtain the maximum value and assign it to $max.
2. Define a reasonable cache time.

Copy code The code is as follows:
$interval=60*60*6

Reasonable The value comes down to the page itself, the number of visitors and the frequency of page updates, which for the above code is 6 hours.
3. Send the Last-Modified header.

Copy code The code is as follows:
header("Last-Modified:".gmdate("r",($max+ $interval)));

When the gmdate() function uses the parameter "r", it will return the corresponding date format according to the HTTP specification.
4. Set the Expires header.

Copy code The code is as follows:
header ("Expires: " . gmdate ("r", ($max + $ interval)));

5. Set the Cache_Control header.

Copy code The code is as follows:
header ("Cache-Control: max-age=$interval");

http://www.bkjia.com/PHPjc/322444.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322444.htmlTechArticleThis involves 4 header types: Last-Modified (last modification time); Expires (validity period); Pragma (compilation directive); Cache-Control (cache control); The first three headers belong to the HTTP1.0 standard. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn