PHP advanced application: header () function setting browser cache _ PHP Tutorial

Source: Internet
Author: User
PHP advanced application: the header () function sets browser cache. 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 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:
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 ");

The padding () function sets browser Cache. four header types are involved: Last-Modified (Last modification time), Expires (validity period), Pragma (Compilation instruction), and Cache...

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: 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.