最后修改 header 固定资源 link 其中资源不时更改?
Last modified header for a fixed resource link where the resource changes from time to time?
目前我正在编写一个用于下载资源的 WordPress 小插件 运行 更新。这意味着我将固定端点 URL 称为:
https://www.example.com/downloads/app/latest
如果调用此 URL,则服务器正在传送所请求资源(例如可执行应用程序)的最新版本。资源的名称可能会有所不同,但也可能与旧版本相同。因此我想把一个有效的最后修改 header.
基本上我从这个问题得到了 headers 设置:Force file download with php using header()
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\'));
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Pragma: public');
header('Content-Length: ' . $size);
现在我想知道 如何正确设置 header 以便浏览器知道资源是否已更改.假设我有所请求资源的最后修改日期,我的猜测是:
header('Cache-Control: must-revalidate');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', $last_modified_time));
如果我不知道最后修改日期是什么,我会输入:
header('Cache-Control: no-cache');
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T'));
这是正确的(尤其是 headers)还是我错过了什么?不确定,因为缓存控制、修改日期、过期等的大量设置。
经过一些研究,我找到了解决我问题的 ETag header。设置 ETag
header 的工作方式如下所述:How to use etags in a PHP file?
还找到了一些关于如何将其与 Expires
header 相结合的信息:ETag vs Header Expires - ETag header 至少发出一个请求来比较文件校验和而 expires header 至少发出一个请求。
因此我现在有了完美的答案(至少我认为):
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\'));
$size = filesize($file);
$etag = hash_file('md5', $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Pragma: public');
header('Content-Length: ' . $size);
header('Cache-Control: no-cache');
header('Expires: 0');
header('ETag: ' . $etag);
目前我正在编写一个用于下载资源的 WordPress 小插件 运行 更新。这意味着我将固定端点 URL 称为:
https://www.example.com/downloads/app/latest
如果调用此 URL,则服务器正在传送所请求资源(例如可执行应用程序)的最新版本。资源的名称可能会有所不同,但也可能与旧版本相同。因此我想把一个有效的最后修改 header.
基本上我从这个问题得到了 headers 设置:Force file download with php using header()
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\'));
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Pragma: public');
header('Content-Length: ' . $size);
现在我想知道 如何正确设置 header 以便浏览器知道资源是否已更改.假设我有所请求资源的最后修改日期,我的猜测是:
header('Cache-Control: must-revalidate');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', $last_modified_time));
如果我不知道最后修改日期是什么,我会输入:
header('Cache-Control: no-cache');
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T'));
这是正确的(尤其是 headers)还是我错过了什么?不确定,因为缓存控制、修改日期、过期等的大量设置。
经过一些研究,我找到了解决我问题的 ETag header。设置 ETag
header 的工作方式如下所述:How to use etags in a PHP file?
还找到了一些关于如何将其与 Expires
header 相结合的信息:ETag vs Header Expires - ETag header 至少发出一个请求来比较文件校验和而 expires header 至少发出一个请求。
因此我现在有了完美的答案(至少我认为):
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\'));
$size = filesize($file);
$etag = hash_file('md5', $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Pragma: public');
header('Content-Length: ' . $size);
header('Cache-Control: no-cache');
header('Expires: 0');
header('ETag: ' . $etag);