Forum Moderators: coopster & phranque

Message Too Old, No Replies

Image is caching, how do i stop it

using gd, php, but browser is loading cache, not image

         

scratch

9:05 am on Jan 29, 2003 (gmt 0)

10+ Year Member



I am using gd to generate images dynamically on a page, with the user able to make changes to said image. Everything works fine, but i have to right-click/reload to see the changes. It always pulls the older version although it is working perfectly.

I am assuming that i have no control over users browsers and how often they check to see if a new page. I want this to be the only thing that caches.

A. Is there a way around using a new filename each time i generate the image.

Meta stuff does not seem to work like the following
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
also the javascript load image (var image = "filename.jpg" or whatever) does not do the trick either.

B. If i have to create new images each page load for the thousands of users, how should i generate unique names for them...and how should i clean up later. An overlap would be dangerous.

I would like to keep the last version if possible so they can copy the url and be able to see it later, and i also can't let myself be overrun with temp images...thats why i would prefer to create just one image...

crypto

10:51 am on Jan 29, 2003 (gmt 0)

10+ Year Member



Try this:

<meta http-equiv="refresh" content="8">

This will refresh the browser every 8 seconds.

For generating unique filenames try appending the time
stamp to the filenames.

andreasfriedrich

12:17 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Send the appropriate header [php.net] along with the image to prevent browsers from caching the image.

Andreas

scratch

4:18 pm on Jan 29, 2003 (gmt 0)

10+ Year Member



<meta http-equiv="refresh" content="8">

Is really cool, but it makes it hard to see the page as the whole page reloads every so many seconds...

How do i add a time stamp to the image, that is the only thing i want to force a reload on.

I am generating a file from within the page that is external and then loading it into an image, so i am not sure i can affect the header without reworking the whole thing.

Is it possible to pass vars to php from html without submitting a page using javascript and then redrawing the image.

scratch

4:28 am on Jan 30, 2003 (gmt 0)

10+ Year Member



$filetocreate = date("U").".jpg"; it is

I give up and must give take crypto's advice for the time stamp. I'll probably code into the form a way to delete previous file as the next one is generated.

I'm really surprised that there does not seem a way to reload a picture for the user.

crypto

10:42 am on Jan 30, 2003 (gmt 0)

10+ Year Member



for unique filename try this:

$filetocreate=rand().date("dmYHis").".jpg";

I think this should be unique. If you want to reload the page on some specific event then try a javascript function for that event using window.location.reload() method or you can set a timer to reload the page periodically.

Brett_Tabke

2:01 pm on Jan 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That is really the hard way to go about it scratch.

Put your graphic or where ever the graphic is being pulled from, in a single directory. Then set the headers in that directory via an .htaccess file:

Header add Cache-Control "max-age=0"
Header add Pragma "no-cache"

That will cause everything in that directory to reload on each browser view.

That is if you are using apache. If not, switch ;-)

scratch

4:14 pm on Jan 30, 2003 (gmt 0)

10+ Year Member



Thank you so, and i am moving to apache right now (from iis and asp) so i will be able to implement the htaccess. I've already got 87 images from debugging alone, this is a much better solution.

Ron_Carnell

5:03 pm on Jan 30, 2003 (gmt 0)

10+ Year Member



You shouldn't have to change the file name. You just have to change the browser's perception of the file name. Add a question mark to the end of the file name, followed by a string of random numbers (current time will do fine). It will look like filename.jpg?012345. The server will ignore the passed parameter, but the browser will consider each instance as a unique file name. This is a very old trick to force pages to bypass the cache, but should work equally well with images.