Search code examples
phpapachecachinghttp-headerspragma

What could be adding "Pragma:no-cache" to my response Headers? (Apache, PHP)


I have a website which maintenance I've inherited, which is a big hairy mess.
One of the things I'm doing is improving performance. Among other things, I'm adding Expires headers to images.

Now, there are some images that are served through a PHP file, and I notice that they do have the Expires header, but they also get loaded every time.

Looking at Response Headers, I see this:

Expires Wed, 15 Jun 2011 18:11:55 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache

Which obviously explains the problem.

Now, I've looked all over the code base, and it doesn't say "pragma" anywhere. .htaccess doesn't seem to have anything related either.

Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?


Solution

  • Create a simple file that includes none of your PHP libraries but lives in the same folder as the file that serves up your images through a PHP file.

    file: test.php
    

    Request this file through a browser and check the headers. If you see the Response headers that you don't want, you know that they're configured via apache and not generated via a PHP file and you can concentrate your searches on .htaccess file in the directory tree, and on the http.confg and other included apache config files. You'll want to search for

    <Directory....
    

    and

    <VirtualHost
    

    sections that may apply to your site.

    If you don't see the headers in a request for that simple PHP file, you know that PHP is setting the headers somewhere. At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)

    var_dump(get_included_files());
    

    Request an image through the image serving URL. That above snippet will print out all the PHP files used in the request. (you'll probably need to view source or use curl to see the raw output, as the browser will report an invalid image)

    Having a subset of your files to work file, search through them for calls to the

    header();
    

    function. The header function is the only way (I think) that raw PHP code can set Response headers. You'll also want to search for

    call_user_func
    eval
    $$
    

    in case there's any dynamic code on the page that's using PHP's meta-programming capabilities to call the header function.

    Good luck!