Caching PHP output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dr.Goodvibes
Forum Newbie
Posts: 19
Joined: Wed May 24, 2006 10:14 am
Location: New Zealand

Caching PHP output

Post 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]
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Dr.Goodvibes
Forum Newbie
Posts: 19
Joined: Wed May 24, 2006 10:14 am
Location: New Zealand

It now works ... I think.

Post 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]
Post Reply