Php static content cache time setting method

Source: Internet
Author: User
This article describes how to set the cache time for static content in php. it involves the application techniques for parameters in the header function and is of great practical value, for more information about how to set the cache time for static content in php, see the following example. 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.

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.