views:

103

answers:

2

What I'm doing

I'm pulling an image from the database and sending it to the browser with all the proper headers - the image displays fine. I also send an ETag header, using the SHA1 of the image's content as the tag.

The images are getting called semi regularly, so caching is a bit of an issue (won't kill the site, but nice to have).

The Problem

$_SERVER['HTTP_IF_NONE_MATCH'] is not available to me. As far as I can tell, this is because of PHP's "disobey the cache controls" life style. I can't mess with the session cache limiter, because I don't have access. But, even if I did have access, I wouldn't want to touch it: 99% of the site is under WordPress.

The Environment

  • PHP 4 (don't ask)
  • Apache 2.2
  • WordPress
  • The images live in the database (largeblog), which I can't change.

Any guidance, tip/tricks, etc. would be helpful. I don't have much room to change the environmental/structural stuff.

Cheers.

A: 

If PHP is not receiving The If-None-Match match header, there's not much you can do. I don't know what you mean by "PHP's "disobey the cache controls""; PHP generates arbitrary dynamic on-the-fly, it cannot, a priori, know whether what it is to return is cached by the client or not.

Anyway, you should investigate whether the client is in fact SENDING the header. If it is, but it's not reaching PHP, check whether it's reaching Apache. If it's reaching PHP but not Apache, you could always hack some solution with mod-rewrite, like adding the header as query string (not tested!):

RewriteCond %{HTTP_IF_NONE_MATCH} (.+)
RewriteRule ^/get_image.php /get_image.php?if-none-match=%1 [B,QSA]
Artefacto
+2  A: 

Have you tried reading HTTP_IF_NONE_MATCH from apache_request_headers()?

If you are running pre-4.3 php, it was called getallheaders() before.


Edit I now see, in the page I linked, that you may also want to try to put

RewriteEngine on
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

in the appropriate .htaccess file to force Apache to set the PHP $_SERVER[...] variables you're unsuccessfully trying to read.

MartinodF