2011-04-25 27 views
2

任何人都可以幫助我設置使用PHP的Expire頭文件, .htaccess不好,因爲我的主機不會在apache上啓用mod_expires。使用PHP設置Expire頭文件

所以基本上我正在尋找一種方式來做到:

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代碼後的頭,並從私人會議採取:

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 
+2

「什麼都沒有發生」?你的意思是,它沒有輸出標題? – deceze 2011-04-25 09:23:23

+0

是的,請添加更多細節。你在哪裏插入這個PHP代碼? – 2011-04-25 09:25:20

+0

好吧,檢查Yslow或PageSpeed我看到瀏覽器chaching筆記,我將代碼插入我的index.php頂部,並且我沒有收到任何錯誤作爲標題發送。因此檢查Net標籤我看到:Expires \t Mon,1 2001 2001 00:00:00 GMT Last-Modified \t 2011年4月25日星期一09:05:17 GMT 緩存控制\t無存儲,無緩存,必須重新驗證,後檢查= 0,預檢查= 0 Pragma \t no-cache – alex 2011-04-25 09:30:16

回答

0

,可以幫助我對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'); 
+0

是啊試過了,仍然沒有改變標題... – alex 2011-04-25 09:31:42

+0

你有輸出之前設置標題? do:error_repoerting(E_ALL);並尋找關於標題 – 2011-04-25 09:33:35

+0

實際上我沒有訪問我的PHP。ini,所以我使用的主機設置的錯誤,但我猜標題發送錯誤應該彈出,如果標題不正確 – alex 2011-04-25 09:34:53

10

根據問題的評論它看起來您的系統運行的PHP設置爲session.cache_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頭你提到的略有不同(據我所知的Joomla使用了很日期時間,如果我沒有記錯的話)。但總的來說,這並沒有什麼區別,因爲這兩個日期都是過去的。

你應該session_cache_limiter(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替換每個header指令。

+0

好吧,這是行不通的。我應該看到一個內容類型的頭到期爲JPG圖像,對吧?以及它不是在請求/響應 – alex 2011-04-25 18:13:58

+0

是的,你應該看過腳本的標題。您能否清除緩存和Cookie,再次執行代碼並在執行後向我顯示第一個原始HTTP響應?你有Fiddler或s/t來監視HTTP通信,對吧?如果你正在像Zend Framework這樣的PHP框架下運行,或者僅僅使用PHP,這可能會有幫助。 – 2011-04-25 18:40:04

+0

我正在工作的網站是基於joomla,在本地主機上設置我的apache來運行mod_expires和htaccess我得到過期,問題出現在主機上。我使用tamperdata或螢火蟲上的網絡面板來查看我的請求 – alex 2011-04-25 19:44:13

相關問題