NYCPHP Meetup

NYPHP.org

[nycphp-talk] Overwriting Cache Headers

Michael B Allen ioplex at gmail.com
Sun Sep 14 12:25:18 EDT 2008


On Sun, Sep 14, 2008 at 11:15 AM, Tom Melendez <tom at supertom.com> wrote:
> Have you tried overwriting the header with the header() function,
> either by making it blank ("Pragma:") or with the desired value?

Hi Tom,

That sort of works. What it does is actually send blank headers to the
client like:

   Cache-Control: \r\n
   Pragma: \r\n

But Firefox at least seems to handle this correctly.

[Note: Firebug does not show you what was received on the network. It
shows you what FF knows from the communication. This is actually good
IMO but if you want to know what was really sent out on the network
you'll want to double check with WireShark.]

Also, in the process of Googling this issue I read some forum posts
that claimed a blank header generated an error. But with at least PHP
5.2.6, calling header('Pragma:') doesn't seem to generate any errors
that I can see so far. Or perhaps the posts were just wrong (e.g.
actually a headers-already-sent problem).

Anyway, from playing around with this, I have finally settled on the
following to enable caching of PHP output on the client:

  header('Expires: ' . gmdate('D, d M Y H:i:s ', time() + (60 * 60)) . 'GMT');
  header('Cache-Control:');
  header('Pragma:');

At least with Firefox it seems caching is largely controlled by the
Expires header. Note that I set the expiration for one hour. And I've
only tried this will application/x-javascript and text/css output with
Content-Disposition: filename=<filename>.

Mike

> On Sun, Sep 14, 2008 at 12:21 AM, justin <justin at justinhileman.info> wrote:
>> 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
>> _______________________________________________
>> New York PHP Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>>
>> NYPHPCon 2006 Presentations Online
>> http://www.nyphpcon.com
>>
>> Show Your Participation in New York PHP
>> http://www.nyphp.org/show_participation.php
>>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>



-- 
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/



More information about the talk mailing list