Use the header () function in PHP to set the browser cache code.

Source: Internet
Author: User
PHP advanced application learning notes: Use the header () function to set 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:
Command Description
Public Can be cached anywhere
Private Can only be cached by browsers
No-cache Cannot be cached anywhere
Must-revalidate The cache must check the updated version.
Proxy-revalidate Agent cache must check the updated version
Max-age The period in seconds when the 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:
The code is as follows:
// 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.
The code is as follows:
$ 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.
The code is as follows:
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.
The code is as follows:
Header ("Expires:". gmdate ("r", ($ max + $ interval )));

5. set the Cache_Control header.
The code is as follows:
Header ("Cache-Control: max-age = $ interval ");

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.