要使用PHP控制浏览器缓存,您可以通过设置HTTP头信息来实现。以下是一些常用的方法:
禁用浏览器缓存:
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
设置缓存过期时间:
$expires = 60 * 60 * 24 * 7; // 1周
header("Cache-Control: max-age=" . $expires);
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expires) . " GMT");
设置特定文件的缓存过期时间:
$expires = 60 * 60 * 24 * 7; // 1周
$file = 'path/to/file.jpg';
header("Cache-Control: max-age=" . $expires);
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expires) . " GMT");
readfile($file);
请注意,这些代码应该在输出任何内容之前执行,以确保HTTP头信息正确设置。
此外,您还可以使用.htaccess文件来控制缓存。例如,以下代码将在Apache服务器上禁用缓存:
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
希望这些信息对您有所帮助!
下一篇:PHP如何读取COOKIES
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站