This article mainly introduces how to set the cache time for static content in php. It involves the application skills for parameters in the header function and is very useful. For more information, see
This article mainly introduces how to set the cache time for static content in php. It involves the application skills for parameters in the header function and is very useful. For more information, see
This example describes how to set the cache time for static content in php. Share it with you for your reference. The specific method is analyzed as follows:
When using the Baidu tool for a small test, we were prompted that we had to set the static content cache time. I had no server permission to operate on myself and had to start from other aspects, after I searched for php, I found that I could use the header function to cache pages in the browser.
Set the static content cache time. The Code is as follows:
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. The following describes the four lines of code. The Code is as follows:
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 ads ';
}
Else if ($ aid = 1)
{
Echo 'Load ad content ';
}
The first line: $ interval tells you that it has been set for six hours. You can customize it here.
Row 2: Send a Last-Modified request to the client browser, which calls the corresponding date based on the gmdate parameter r.
Row 3: The expiration time of Expires is set.
Row 4: Set the max-age = $ interval Date of Cache-Control.
The test result is excellent.
I hope this article will help you with PHP programming.
,