PHP advanced application header () function sets browser cache. four header types are involved: Last-Modified (Last modification time), Expires (validity period), and Pragma (compilation instructions ); cache-Control (Cache Control); the first three headers belong to the HTTP1.0 standard. The header uses the UTC date and time value for Last-Modified. If the header () function of PHP advanced application sets browser cache
Four header types are involved:
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 header uses the UTC date and time value for Last-Modified. If the cache system finds that the Last-Modified value is more connected than the page cache version
Near the current time, he knows that the new version from the server should be used.
Expires indicates when the cache version should expire (Greenwich Mean Time ). Setting it to a previous time will force the use of the page on the server.
How can Pragma process page data. This avoids page caching:
Header ("Pragma: no-cache ");
The Cache-Co0ntrol header label is added in HTTP1.1 to achieve more detailed control (you should continue to use the HTTP1.0 header ). The Cache-Control settings include:
Many, as shown in the following table:
Description
Public can be cached anywhere
Private can only be cached by the browser
No-cache cannot be cached anywhere
Must-revalidate the cache must check the updated version
Proxy-revalidate proxy cache must check the updated version
The period in seconds when the max-age content can be cached.
S-maxage overwrite the max-age settings of the shared cache
The following example uses header () to set the browser cache:
// Connect to the database:
$ Dbc = @ mysqli_connect (localhost, username, password, test) OR die (
Cocould 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 ");
?>
1. connect to the database and obtain the latest date value date_added and date_completed in the data table. use the UNIX_TIMESTAMP () function to convert the returned value to an integer and then obtain the maximum value and assign it to $ max.
2. define a reasonable cache time.
$ Interval = 60*60*6
The reasonable value is the page itself, the number of visitors, and the page update frequency. the code above is 6 hours.
3. send the Last-Modified header.
Header ("Last-Modified:". gmdate ("r", ($ max $ interval )));
When the gmdate () function uses the "r" parameter, the corresponding date format is returned according to the HTTP specification.
4. set the Expires header.
Header ("Expires:". gmdate ("r", ($ max $ interval )));
5. set the Cache_Control header.
Header ("Cache-Control: max-age = $ interval ");