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.