php - 使用 PHP 设置过期 header

标签 php header

谁能帮助我仅使用 PHP 设置过期 header , .htaccess 不好,因为我的主机不会在 apache 上启用 mod_expires。

所以基本上我正在寻找一种方法:

Expire Header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 365 days"
</FilesMatch>

仅使用 php。

对不同的文件类型设置不同的过期期限也很重要,所以我尝试使用类似的东西:

header ("content-type: image/jpg; charset: UTF-8");
header ("cache-control: must-revalidate");
$offset = 48 * 60 * 60;
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);

对于每种类型的文件,但没有任何反应。


添加 PHP 代码后的 header ,取自私有(private) session :

Response Headersview source
Date    Mon, 25 Apr 2011 19:47:10 GMT
Server  Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.1
P3P CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Expires Mon, 1 Jan 2001 00:00:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Last-Modified   Mon, 25 Apr 2011 19:47:10 GMT
Content-Encoding    gzip
Pragma  no-cache
Keep-Alive  timeout=5, max=100
Connection  Keep-Alive
Transfer-Encoding   chunked
Content-Type    text/html; charset=utf-8
Request Headersview source
Host    localhost
User-Agent  Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.10 (maverick) Firefox/3.6.16 ( .NET CLR 3.5.30729) FirePHP/0.5
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.7,he;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Referer http://localhost/-----------------
Cookie  fboard_settings[current_view]=flat; style_cookie=null; phpbb3_4s1go_k=; phpbb3_4s1go_u=2; phpbb3_4s1go_sid=8a3835a63834e9851b0cde3e2f6cff63; jw_clean_pro_tpl=jw_clean_pro; acpSearchCookie[searchphrase]=any; acpSearchCookie[acpSearch]=%D7%97%D7%A4%D7%A9+...; acpSearchCookie[cid]=0; acpSearchCookie[field_city]=0; 14a2bb08766d6180968b7925b7902d70=bgd3h1uj5dctoevtdiaj1jtmg6; 3e2fd857422e2463a01f9631f718017a=nbdjbmjsn9ee8ng90ui816hec2
x-insight   activate

最佳答案

根据您在问题中的评论,您的系统似乎正在使用 session.cache_limiter = nocache 的 PHP 设置运行。这将自动发送以下 header :

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

只有您提到的 Expires header 略有不同(如果我没记错的话,afaik Joomla 使用的是那个日期时间)。但总而言之,这没什么区别,因为这两个日期都是过去的。

您应该尝试在代码中使用 session_cache_limiter(false); 来阻止 PHP 发送其默认缓存 header :

function sendHeader($sType, $iLastModified, $iSecondsToCache)
{

    $aType = array(
        'ico'   => 'image/x-icon',
        'jpg'   => 'image/jpeg',
        'png'   => 'image/png',
        'gif'   => 'image/gif',
        'js'    => 'text/javascript',
        'css'   => 'text/css',
        'swf'   => 'application/x-shockwave-flash'
    );
    if (!isset($aType[$sType]))
        die('No mime type found for ' . $sType);

    //$sLastModified = gmdate('r', $iLastModified);
    $sLastModified = gmdate('D, d M Y H:i:s', $iLastModified) . ' GMT';

    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
    {
        if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $sLastModified)
        {
            header('HTTP/1.1 304 Not Modified');
            exit;
        }
    }

    session_cache_limiter(false);
    //header('Expires: ' . gmdate('r', $iLastModified + $iSecondsToCache));
    header('Expires: ' . gmdate('D, d M Y H:i:s', $iLastModified + $iSecondsToCache) . ' GMT');
    header('Cache-Control: public');
    header('Last-Modified: ' . $sLastModified);
    header('Content-Type: ' . $aType[$sType]);

}

date_default_timezone_set('Europe/Berlin');

$iLastModified = strtotime('2011-04-25 07:08:09');
$iSecondsToCache = 48 * 60 * 60;
sendHeader('jpg', $iLastModified, $iSecondsToCache);

// stream sample image/jpeg content
$rGD = imagecreatetruecolor(100, 20);
$iColor = imagecolorallocate($rGD, 255, 255, 255);
imagestring($rGD, 1, 5, 5,  'Image to be cached', $iColor);
imagejpeg($rGD);
imagedestroy($rGD);
exit;

编辑:

与此同时,对我来说更有可能是 Joomla 导致了问题。如果测试代码可以访问 Joomla 库,请尝试插入:

jimport('joomla.environment.response');
JResponse::allowCache(true);

在函数的最顶部,将每个 header 指令替换为 JResponse::setHeader

关于php - 使用 PHP 设置过期 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5776971/

相关文章:

c++ - 创建dll文件是否一定要写头文件?

c - 如何解决头文件之间的这种相互依赖关系?

使用 BOOL 函数的 C 头文件

php - NOW() BETWEEN date_from AND date_to 不工作?

php - 给 Facebook 好友评分

php - 作业调度问题

javascript - 在 php 中获取 Bootstrap slider 的值?

Angular Material 排序表 - 如何将标题文本右对齐?

javascript - 使用 get 请求头向服务器发送数据

php - 仅在 WooCommerce 中自定义类型的管理产品上添加字段