Home Backend Development PHP Tutorial PHP advanced application: header() function sets browser cache_PHP tutorial

PHP advanced application: header() function sets browser cache_PHP tutorial

Jul 13, 2016 pm 05:41 PM
header php function application Browser cache set up advanced

The header() function of PHP advanced application sets the browser cache

This involves 4 header types:
Last-Modified (last modification time);
Expires( Validity period);
Pragma (compilation directive);
Cache-Control (cache control);
The first three headers belong to the HTTP1.0 standard. The Last-Modified header uses UTC date and time values. If the caching system sees that the Last-Modified value is closer to the current time than the cached version of the page, it knows it should use the new version from the server.

Expires indicates when the cached version should expire (GMT). Setting this to a previous time will force the page on the server to be used.

Pragma defines how the page data should be processed. You can avoid caching the page like this:

header("Pragma:no-cache");

Cache-Co0ntrol The header was added in HTTP1.1 and can achieve more detailed Control (should also continue to use HTTP 1.0 headers). There are many
settings for Cache-Control, as shown in the following table:
directive                                                                                                                            Cannot be cached anywhere
must-revalidate The cache must check for an updated version
proxy-revalidate The proxy cache must check for an updated version
max-age The period during which the content can be cached, expressed in seconds
s-maxage Overrides the max-age of the shared cache Age setting
The following example uses header() to set the browser's cache:
// 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, obtain the latest date values ​​date_added and date_completed in the data table, use the UNIX_TIMESTAMP() function to convert the return value into 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 depends on the page itself, the number of visitors and the page update frequency. The above code is 6 hours.

3. Send the Last-Modified header.
header("Last-Modified:".gmdate("r",($max $interval)));
When the gmdate() function uses the parameter "r", it will return the corresponding date according to the HTTP specification Format.

4. Set the Expires header.
header ("Expires: " . gmdate ("r", ($max $interval)));

5. Set the Cache_Control header.
header ("Cache-Control: max-age=$interval");




http://www.bkjia.com/PHPjc/486132.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/486132.htmlTechArticlePHP advanced application header() function sets browser cache, which involves 4 header types: Last-Modified (Last modification time); Expires (validity period); Pragma (compilation instructions); Cache...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? Apr 05, 2025 pm 10:33 PM

Using locally installed font files in web pages Recently, I downloaded a free font from the internet and successfully installed it into my system. Now...

Why does negative margins not take effect in some cases? How to solve this problem? Why does negative margins not take effect in some cases? How to solve this problem? Apr 05, 2025 pm 10:18 PM

Why do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

How to customize the resize symbol through CSS and make it uniform with the background color? How to customize the resize symbol through CSS and make it uniform with the background color? Apr 05, 2025 pm 02:30 PM

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes? How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes? Apr 05, 2025 pm 06:06 PM

Implementing responsive layouts using CSS When we want to implement layout changes under different screen sizes in web design, CSS...

How to obtain real-time application and viewer data on the 58.com work page? How to obtain real-time application and viewer data on the 58.com work page? Apr 05, 2025 am 08:06 AM

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

See all articles