Home >Backend Development >PHP Tutorial >How to set static content cache time in php, set static cache in php_PHP tutorial

How to set static content cache time in php, set static cache in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:291124browse

How to set static content cache time in php, set static cache in php

The example in this article describes how to set the cache time of static content in PHP. Share it with everyone for your reference. The specific method analysis is as follows:

When using Baidu tools to do a small test, it was prompted that we need to set the static content cache time. I don’t have server permissions to operate it, so I can only start from other aspects. I am learning PHP myself and searched and found that I can use the header function. Implement the time when the browser caches the page, as follows.

Set the static content cache time, the code is as follows:

Copy code The code is as follows:
$interval = 60 * 60 * 6; // 6 hours
header ("Last-Modified: " . gmdate ('r', $max));
header ("Expires: " . gmdate ("r", ($max + $interval)));
header ("Cache-Control: max-age=$interval");

We add the above code to the beginning of the php file. Let me explain these four lines of code in detail. The code is as follows:
Copy code The code is as follows:
$interval = 60 * 60 * 6; // 6 hours
header ("Last-Modified: " . gmdate ('r', $max));
header ("Expires: " . gmdate ("r", ($max + $interval)));
header ("Cache-Control: max-age=$interval");
$aid = intval( isset( $_POST['aid'] )?$_POST['aid']:0 );
if( ! $aid )
{
echo 'undefined advertisement';
}
else if( $aid ==1 )
{
echo 'Load advertising content';
}

The first line: $interval tells you to set 6 hours. You can customize it here.

The second line: Send a Last-Modified request to the client browser, which will call the corresponding date based on the parameter r of gmdate.

The third line: Set the Expires expiration time.

Line 4: Set the max-age=$interval date of Cache-Control.

The retest result is excellent.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920622.htmlTechArticleHow to set the cache time of static content in php, set the static cache in php. This article explains how to set the cache time of static content in php. method. Share it with everyone for your reference. Specific method analysis...
Statement:
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