PHP using the header () function to set the browser cache code _php tips

Source: Internet
Author: User
Tags browser cache
This involves 4 kinds of header types:

Last-modified (last modification time);
Expires (valid term);
Pragma (compiling instructions);
Cache-control (cache control);
The first three headers belong to the HTTP1.0 standard. The header last-modified uses a UTC datetime value. If the cache system finds that the Last-modified value is more connected than the page cache version
In the near current time, he knew that a new version should be used from the server.

Expires indicates when the cached version should expire (Greenwich Mean Time). Setting it to a previous time will force the use of pages on the server.

pragma life. How the page data should be processed. You can avoid caching a page in this way:

Header ("Pragma:no-cache");

Cache-co0ntrol headers are added in HTTP1.1 to achieve finer control (and should continue to use HTTP1.0 headers). The Cache-control settings have
Many, as in the following table:
Instructions Meaning
Public Can be cached in any place
Private Can only be cached by the browser
No-cache cannot be cached anywhere
Must-revalidate Cache must check for newer versions
Proxy-revalidate Proxy cache must check for newer versions
Max-age The period in which content can be cached, expressed in seconds
S-maxage Overwrite max-age settings for shared cache
The following example uses the header () to set the browser's cache:
Copy Code code as follows:

<?php # Script 2.7-view_tasks.php
Connect to the database:
$DBC = @mysqli_connect (' localhost ', ' username ', ' password ', ' Test ') OR die (' <p>could not connect to the database!& Lt;/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. When you connect to the database, get the latest date value date_added,date_completed in the datasheet, convert the return value to an integer with the Unix_timestamp () function, and then get the maximum value given to $max.
2. Define a reasonable cache time.
Copy Code code as follows:

$interval =60*60*6

Reasonable value of the page itself, the number of visitors and the update frequency of the page, the above code is 6 hours.
3. Send last-modified headers.
Copy Code code as follows:

Header ("Last-modified:" Gmdate ("R", ($max + $interval));

When the Gmdate () function uses the parameter "R", the corresponding date format is returned according to the HTTP specification.
4. Set the Expires header.
Copy Code code as follows:

Header ("Expires:"). Gmdate ("R", ($max + $interval));

5. Set the Cache_control header.
Copy Code code 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.