add "EXPIRE" tag in header while serving content
does adding "EXPIRE" tag in header forces the brows开发者_运维技巧er to cache the content till the time expired ?
How to do so while serving an static image/css/js in PHP ?You can use header and gmdate functions:
// Actualy date in GTM 0
header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
// Las modify date (now, for example)
header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
// The expire time (one hour in the future) <-- sorry my english!!!
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
Always make sure to send headers before sending data, ex:
// GOOD!
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
echo "content";
// BAD!
echo "some content";
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
If you need generate the content before send header, you can use a ob functions:
ob_start();
echo "content";
echo "more content";
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
ob_end_flush();
For static content use web server config. For apache it's .htaccess, for iis it's web.config.
精彩评论