How PHP sets the time for static content caching _php Tips

Source: Internet
Author: User
Tags learn php browser cache

This example describes how PHP sets the time for static content caching. Share to everyone for your reference. The specific methods are analyzed as follows:

In the use of Baidu tools to make a small test prompts us to set the static content cache time, I do not have the server permissions to operate, can only start from other aspects, learn PHP I search after found that you can use the header function to implement browser cache page time, specific as follows.

Set the static content cache time with the following code:

Copy Code code 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, below to give you a detailed explanation of the four lines of code, the code is as follows:
Copy Code code 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 ad ';
}
else if ($aid ==1)
{
echo ' load ad content ';
}

The first line: $interval tell you to set 6 hours, here everyone can be customized.

Second line: Sends a last-modified request to the client browser, which invokes the corresponding date according to the gmdate's parameter R.

Third line: Set the Expires expiration time.

Line four: Set the Cache-control max-age= $interval date.

The test results are excellent.

I hope this article will help you with your PHP program design.

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.