Page 1 of 1

Caching PHP output

Posted: Thu May 25, 2006 5:22 am
by Dr.Goodvibes
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have created a web application to display images which have been stored in a mySQL blob.

To view the image I call the PHP script from within an HTML img tag.

Code: Select all

<img src='viewImg.php?imID=2345.gif' alt='Please cache me' />
note to self - Saw somewhere this creates a new process each time it is accessed, which is probably not good. :cry:

The script then retrieves the blob from the database, checks for a header type and displays the image.

No problem thus far, HOWEVER:

I am not able to cache the image.

Any ideas.

This is the header I currently have.

Code: Select all

list($width, $height, $mimeType, $attr) = getimagesize($imgBlob);
header("Content-Type: ".image_type_to_mime_type($mimeType));
header("Last-Modified:  Wed, 24 May 2006 01:01:00 GMT"); /* Yesterday */ 
header("Cache-Control: max-age=86400, must-revalidate");
echo $imgBlob;
I have checked the firefox (and Opera) cache and nothing is listed.
Before I clear the cache in firefox I did see some of the 'files' with an expire date of 1970-01-01 13:00:00

Any thoughts?

Thanks for any help.


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu May 25, 2006 10:50 am
by BDKR
First things first, what is the reason for storing the images in the database?

Anyway, the "must-revalidate" portion of the header should not be there if you want the browser to cache information. Err..., it's kind of self explanatory. :oops:

So what exactly are you doing that you need all this clever crap?

Cheers,
BDKR

It now works ... I think.

Posted: Thu May 25, 2006 12:08 pm
by Dr.Goodvibes
OK. I think I solved it. :idea:

I've been messing around for the last hour or so.

Tried all sort of things which didn't work, so I continued building the overall application and ..BING.

The code I now have is as follows:

img.php (display image form DB)

Code: Select all

list($width, $height, $mimeType, $attr) = getimagesize($imgBlob);
    header("Content-Type: ".image_type_to_mime_type($mimeType));
    header("Cache-Control: max-age=86400, must-revalidate");
    header("Content-Length: ".filesize($imgBlob));
    echo $imgBlob;
As I'm building myself a application of extra ordinary magnitude, I've been adding my building blocks around the code.
One of these is the build header object. It includes:


imageCaller.php (Calls the image display script)

Code: Select all

header ("Cache-Control: cache, max-age=259200"); 
      $offset = 60 * 60 * 24 * 3;
      $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
      header($ExpStr);
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
The above is now included in the header of the script that is calling img.php. This may have been the answer.

I know the caching times don't quite support each other, but it works. :D

Oh I think I do remember from way back that time zones can be a problem too.

I think the must-revalidate assists in renewing the cached file after the given time has expired and can be in the statement. It doesn't mean no-cache. The following document explains it better than I can.


http://www.mnot.net/cache_docs/


Why am I doing this?

Uhm, cause I can't seem to find a way to create temporary files from the web as I haven't worked out a way to enable a root process to change chmod(). Probably need to create a cgi or something. Another day.

And the images are only thumbs so they're not going to kill the mySQL cache on large queries...hopefully.

Hopefully some of this makes sense, as it's now 04:40 in the morning.

[/url]