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

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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!

Answer:

The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.

Answer:

I had a similar problem with Pragma: nocache

session_cache_limiter(false); prior to session_start(); seemed to suppress it.

Answer:

Try unsetting the headers in .htaccess. The below example will unset them for all files matching the extensions ico, jpeg, png, gif, js, css:

<FilesMatch "\.(ico|jpeg|png|gif|js|css)$">
    Header unset Cache-Control
    Header unset Pragma
</FilesMatch>

You can find some hints in this article.

Answer:

I did this at runtime with this:

header("Pragma:");

which forced the script to unset the Pragma header.

Answer:

If it’s not in .htaccess it may be in the main Apache config file – or one of its includes, depending on the setup.

Answer:

It’s worth noting for people with similar problems that many frameworks will auto-add headers especially caching ones. It’s fairly easy to overload them either in the framework library or within your app.