添加“;EXPIRE”;标记


add "EXPIRE" tag in header while serving content

在标头中添加"EXPIRE"标记是否会迫使浏览器缓存内容直到时间到期
在PHP中提供静态图像/css/js时如何做到这一点?

您可以使用标头和gmdate函数:

// 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)); 

发送数据前一定要发送标题,例如:

// 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));

如果你需要在发送标题之前生成内容,你可以使用ob函数:

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();

对于静态内容,请使用web服务器配置。对于apache,它是.htaccess,对于iis,它是web.config.