filip
filip

Reputation: 3084

Caching css, js, txt files parsed by 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");
?>

I am then adding an instruction to my .htaccess file to parse this css file with PHP:

<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.

Here are the headers for main-real.css if accessed directly:

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

Here are the headers for the PHP parsed 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

I have tried modifying the http-headers in all sorts of ways, adding max-age, last-modified and others with no success. Is there something I am missing or misunderstanding?


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

Upvotes: 3

Views: 1864

Answers (2)

mattbasta
mattbasta

Reputation: 13709

You need to look at the inbound HTTP headers and determine if the CSS file has legitimately been changed in that time. That means that you're going to be looking at If-Modified-Since in the request headers. Here's some code that'll do it for you:

$last_modified = filemtime("main-real.css");
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
    $expected_modified = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"]));
    if($last_modified <= $expected_modified) {
        header("HTTP/1.0 304 Not Modified");
        return;
    }
}

Upvotes: 1

Pekka
Pekka

Reputation: 449385

What headers Apache would send for main-real.css is irrelevant, because you are include() ing that file through the file system.

You need to send the same headers through your PHP script before you include the other file.

header("Cache-Control: ........ ");
header("Expires: ....... ");
....
include("main-real.css"); 

Upvotes: 1

Related Questions