使用PHP设置过期标头

使用PHP设置过期标头,php,header,Php,Header,有人能帮我设置只使用PHP的Expire头吗, .htaccess不好,因为我的主机不会在apache上启用mod_expires 因此,基本上我正在寻找一种方法: Expire Header <FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$"> ExpiresDefault "access plus 365 days" </FilesMatch> 对于每种类型的文件,但什么都没有发生 添加PHP代码后的标题,取自私有

有人能帮我设置只使用PHP的Expire头吗, .htaccess不好,因为我的主机不会在apache上启用mod_expires

因此,基本上我正在寻找一种方法:

Expire Header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 365 days"
</FilesMatch>
对于每种类型的文件,但什么都没有发生


添加PHP代码后的标题,取自私有会话:

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

这有助于我进行ajax查询:

header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s',time()+60*60*8 ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );

根据您在问题中的评论,您的系统运行的PHP设置为
session.cache\u limiter=nocache
。这将自动发送以下标题:

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
头有点不同(如果我没有弄错的话,afaik Joomla使用的就是这个日期时间)。但总的来说,这没有什么区别,因为两个日期都是过去的

您应该尝试使用
会话缓存限制器(false)
在代码中,要停止PHP发送其默认缓存头,请执行以下操作:

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

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

“什么都没发生”?你是说,它没有输出标题?是的,请添加更多细节。你到底在哪里插入这段PHP代码?好吧,检查Yslow或PageSpeed,我看到浏览器的chaching注释,我将代码插入顶部的index.PHP中,在发送头时没有收到任何错误。因此,检查我看到的Net选项卡:Expires Mon,2001年1月1日00:00:00 GMT上次修改Mon,2011年4月25日09:05:17 GMT缓存控制无存储,无缓存,必须重新验证,后检查=0,预检查=0 Pragma无缓存在设置标头之前是否有输出?do:错误报告(E_全部);查找一些关于Headers的信息实际上我没有访问php.ini的权限,所以我使用主机设置查找错误,但我猜headers sent error应该会弹出,若header不正确,若error_reporting设置为e_NONE,你们将看不到警告尝试创建一个只有这些行的空php文件并查看headers实际上我正在使用TamperData查看我的header,我看到有缓存控制:并没有存储,并没有缓存。。。另外,尝试将内容类型更改为image/jpg,但作为回应,我仍然使用text/htmlwell,这不起作用。我应该看到jpg图像的内容类型标题为expire,对吗?这不是在request/responseYep中,您应该已经看到了脚本的标题。能否请您清除缓存和cookie,再次执行代码,并显示执行后的第一个原始HTTP响应?你用Fiddler或s/t监控HTTP通信,对吗?了解您是否在某些PHP框架(如Zend framework或bare PHP)下运行可能会有所帮助。我正在使用的站点基于joomla,在本地主机上设置我的apache运行mod_expires,使用htaccess我获得expires,问题出在主机上。我使用tamperdata或firebug上的网络面板查看我的请求您是否正在本地主机上测试答案的代码?不管怎样,您添加了标题。您在测试之前禁用了
mod\u expires
,是吗?
jimport('joomla.environment.response');
JResponse::allowCache(true);