PHP advanced application: header () function to set browser cache

Source: Internet
Author: User

PHP advanced application header () function setting 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:
<? Php # Script 2.7-view_tasks.php
// Connect to the database:
$ Dbc = @ mysqli_connect (localhost, username, password, test) OR die (<p> cocould not connect to the database! </P> </body> // 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 ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.