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

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

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

Expires indicates when the cached version should expire (Greenwich Mean Time). Setting it to a previous time forces the page on the server to be used.

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

Header ("Pragma:no-cache");

The Cache-co0ntrol header is added in the HTTP1.1 to allow finer control (and should continue to use the HTTP1.0 header). The Cache-control settings are
Many, such as 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 updated versions
Proxy-revalidate The 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 sets the browser's cache using the header ():
Copy CodeThe code is as follows:
Connect to the database:
$DBC = @mysqli_connect (' localhost ', ' username ', ' password ', ' Test ') OR die ('

Could 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. After connecting to the database, get the latest date value date_added,date_completed in the data table, convert the return value to an integer with the Unix_timestamp () function, and get the maximum value assigned to $max.
2. Define a reasonable cache time.
Copy CodeThe code is as follows:
$interval =60*60*6

The reasonable value of the page itself, the number of visitors and page update frequency, the above code is 6 hours.
3. Send last-modified headers.
Copy CodeThe code is 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 headers.
Copy CodeThe code is as follows:
Header ("Expires:".) Gmdate ("R", ($max + $interval)));

5. Set the Cache_control headers.
Copy CodeThe code is as follows:
Header ("cache-control:max-age= $interval");

http://www.bkjia.com/PHPjc/322444.html www.bkjia.com true http://www.bkjia.com/PHPjc/322444.html techarticle This involves 4 kinds of header types: last-modified (last modified time); Expires (validity period); Pragma (compile instructions); Cache-control (cache control); The first three headers belong to the HTTP1.0 standard. ...

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