I know how to set a basic expires HTTP response header in PHP as follows...
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
However, I want to make it a little more dynamic in that I want to, using PHP, specify an expiration time eight hours ahead of when the user accesses the content. Could someone help me开发者_开发技巧 achieve this?
Thanks in advance!
You can use Cache-Control’s max-age instead that indicated the number of second relative to the response time:
The expiration time of an entity MAY be specified by the origin server using the Expires header (see section 14.21). Alternatively, it MAY be specified using the max-age directive in a response. When the max-age cache-control directive is present in a cached response, the response is stale if its current age is greater than the age value given (in seconds) at the time of a new request for that resource.
An example:
header('Cache-Control: max-age=28800');
Note that if both Expires and Cache-Control’s max-age are present, max-age is preferred over Expires:
If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive.
Use strtotime to make a timestamp in the future, and gmdate to format it as a string in the GMT timezone.
define('EXPIRE_FORMAT', 'D, d M Y H:i:s T');
$expires = gmdate(EXPIRE_FORMAT, strtotime('+8 hours'));
header("Expires: $expires");
精彩评论