Php tarafından çözümlenen önbelleğe alma css, js, txt dosyaları

0 Cevap php

Question

I am running a bit of an experiment and could use some help.
I have created 2 files. main-real.css which is a standard plain ol' css file, and main.css which is parsed by PHP and has an include() which grabs the former real css file.
Here is the code for main.css:

<?php 
include("main-real.css");
?>

Sonra PHP ile bu css dosyasını ayrıştırmak için elimden .htaccess dosyasına bir talimat ekleme:

<FilesMatch "main.css">
AddHandler application/x-httpd-php5 .css
Header Set Content-Type "text/css"
</FilesMatch> 

This works perfectly on my PHP 5.2 server running Apache.
The issue is that this file does not appear to be cached by the browser, or at least does not return a
304 Not Modified Status code like the regular un-PHP-parsed CSS file.

İşte için üstbilgileridir main-real.css doğrudan erişilebilir ise:

RESPONSE HEADERS
    Date..............Thu, 18 Nov 2010 22:10:57 GMT
    Server............Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
    Last-Modified.....Thu, 18 Nov 2010 22:10:23 GMT
    Etag.............."11b010a-26-4955b0e6671c0"
    Accept-Ranges.....bytes
    Content-Length....38
    Content-Type......text/css

REQUEST HEADERS
    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language....en-us,en;q=0.5
    Accept-Encoding....gzip,deflate
    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive.........115
    Connection.........keep-alive
    Cookie.............fc=fcVal=7625790752294348480
    If-Modified-Since..Thu, 18 Nov 2010 22:10:23 GMT
    If-None-Match......"11b010a-26-4955b0e6671c0"
    Cache-Control......max-age=0

İşte çözümlü PHP için üstbilgileridir main.css:

RESPONSE HEADERS
    Date...............Thu, 18 Nov 2010 22:11:11 GMT
    Server.............Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
    X-Powered-By.......PHP/5.2.11
    Content-Type.......text/css
    Keep-Alive.........timeout=5, max=97
    Connection.........Keep-Alive
    Transfer-Encoding..chunked

REQUEST HEADERS
    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language....en-us,en;q=0.5
    Accept-Encoding....gzip,deflate
    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive.........115
    Connection.........keep-alive
    Cookie.............fc=fcVal=7625790752294348480
    Cache-Control......max-age=0

Ben hiçbir başarı ile max-age, modifiye-son ve diğerlerini ekleyerek yollardan her türlü http başlıklarını değiştirerek denedim. Ben eksik veya yanlış anlama bir şey var mı?


Solution & Final Code

The main missing piece of code was that I needed to send the Last-Modified header prior to the include(). This needs to be done within the PHP file itself! I previously tried adding Last-Modified using an .htaccess Header set instruction, and although that does add the appropriate header, it did not trigger caching.
Here is my final code for main.css with far-future Expires headers and Cache-Control for good measure.

<?php
$last_modified = date("D, d M Y H:i:s \G\M\T", filemtime("main-shared.css"));
$expiration = date("D, d M Y H:i:s \G\M\T", strtotime('+1 year'));

header("Cache-Control: public, no-transform");
header("Expires: $expiration");
header("Last-Modified: $last_modified");

include("main-shared.css");
?>

0 Cevap