NYCPHP Meetup

NYPHP.org

[nycphp-talk] Overwriting Cache Headers

justin justin at justinhileman.info
Sun Sep 14 00:21:19 EDT 2008


On Sat, Sep 13, 2008 at 11:41 PM, Michael B Allen <ioplex at gmail.com> wrote:
> I want to send css and js files from a PHP script so I want to allow caching.
>
> But PHP always sends the following headers:
>
> Array
> (
>    [0] => X-Powered-By: PHP/5.2.6
>    [1] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
>    [2] => Cache-Control: no-store, no-cache, must-revalidate,
> post-check=0, pre-check=0
>    [3] => Pragma: no-cache
> )
>
> AFAIK it's not possible to remove or suppress headers but I can of
> course overwrite them.
>
> The question is: with what?
>
> For the Expires header I suppose it could just be the RFC822 time for
> now + 1 hour.
>
> For the Cache-Control header it's less clear. Here's the spec but I'm
> still trying to decipher the meaning of each directive:
>
>  http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
>
> For the Pragma header I'm largely clueless.
>
> Does anyone know what values for these headers would be equivalent to
> not specifying them at all?
>

Is there any particular reason you need PHP to send the files? You do
have a couple more options. I usually use PHP to write a static file,
then let Apache serve it up as an actual css/js file. You can even use
sweet mod_rewrite rules to hit the php file if the requested css/js
file isn't present. If you're just reading a file from disk and
spitting it out with PHP, you might want to have PHP send your
favorite flavor of redirect header instead.

As far as which headers to send, I poked around a bit. You'd be
surprised how many of the big guys just dump the css on every single
page... But a few that served actual files had headers like the
following. Note that all these css files are coming from a CDN. For
what that's worth.


Headers for an Amazon.com css file:

        Cache-Control: max-age=630720000
        Date: Sun, 14 Sep 2008 04:03:45 GMT
        Content-Type: text/css
        Last-Modified: Thu, 28 Aug 2008 00:32:03 GMT
        Server: Server
        X-Cache-Lookup: MISS from cdn-images.amazon.com:8080
        X-Cache: MISS from cdn-images.amazon.com
        Content-Encoding: deflate
        Expires: Wed, 23 Aug 2028 00:39:01 GMT
        Content-Length: 2018
        X-WR-MODIFICATION: Content-Length


And one from Yahoo.com:

        Date: Thu, 11 Sep 2008 21:50:36 GMT
        Cache-Control: max-age=315360000
        Expires: Sun, 09 Sep 2018 21:50:36 GMT
        Last-Modified: Mon, 17 Mar 2008 17:19:43 GMT
        Accept-Ranges: bytes
        Vary: Accept-Encoding
        Content-Type: text/css
        Content-Encoding: gzip
        Age: 195294
        Content-Length: 1921
        Server: YTS/1.17.8


Twitter seems to use a more reasonable cache length, you might try
something like this:

        Date: Sun, 14 Sep 2008 04:07:46 GMT
        Server: Apache
        Last-Modified: Fri, 12 Sep 2008 00:23:22 GMT
        Accept-Ranges: bytes
        Cache-Control: max-age=86400
        Expires: Mon, 15 Sep 2008 04:07:46 GMT
        Vary: Accept-Encoding
        Content-Encoding: gzip
        Content-Length: 6509
        Content-Type: text/css


I wasn't able to find a css file that returned a Pragma header.
Perhaps you could try "Pragma: " ?

justin
-- 
http://justinhileman.com



More information about the talk mailing list