[Webinar] Streamline your web hosting managementRegister Today



Posted on 1999-07-03
Medium Priority
Last Modified: 2008-03-03
..my little PERL program creates a very simple page with an image in it (.jpg), the image changes often, however its name is always the same.
How do I prevent browsers to cache the image avoing to display always the same one?
I tried something like  ?nocache=random  after the image link but it doesn't work.
Any suggestions?
Question by:uffa
  • 2
  • 2
  • 2
  • +2
LVL 10

Expert Comment

ID: 1213803
you should preload it in the <HEAD> section of the web page into a variable and then simply load it from that variable....

Accepted Solution

anthonyu earned 150 total points
ID: 1213804
Cache Control: no-cache
somewhere in the http headers your script sends the client, maybe just before

Expert Comment

ID: 1213805
suppose you use CGI package :

$my_cgi = new CGI;

print $my_cgi->header(-type=>'image/gif',
                     -status=>'402 Payment Required',
               ----> -expires=>'now', <----

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 1213806
..it will be like this?

print "Cache Control: no-cache\n\n";


print "<Cache Control: no-cache>\n";

is the same command works if I put it on a HTML page?

Expert Comment

ID: 1213807
There is something wrong here.  If your ?cache=random is really a random number there is no way that this can be cached anywhere since the part after the ? is part of the URL and could not be cached.   Remember that the random function in most languages generates the same random number every time if not seeded with something that prevents this.

The no-cache method suggested above DOES NOT WORK.  Too many browsers, sadly, do not recognize it, and even those that do seem to do it for the the html of the page but not the embedded images.

I would be willing to bet that you are just not seeding the random with something equivalent to srand().


Expert Comment

ID: 1213808
Well, 2 points:

1)  You would want to use one newline if it is not the last HTTP header, 2 if it is. like

"Content-type: text/html\nCache Control: no-cache\n\n"
"Cache Control: no-cache\nContent-type: text/html\n\n"

Its an HTTP header, so no < or > unless you are doing it in HTML, like <meta http-equiv="Cache Control" content="no-cache"> which may or may not work, I have never tested that method.

2)  The "Cache control: no-cache" header is HTTP/1.1, so any compliant browser should honor it.  I know for a fact that ie5 and netscape 4.5+ do, even on images.  

If for some reason it doesn't work with a browser you need it to, try Pragma: no-cache.  That is HTTP/1.0 and should work on any browser.

Expert Comment

ID: 1213809
I agree with anthonyu that the specs say that it works with those browsers, I just tested with MSIE 5.0 and NN 4.6 and in both cases the images were not re-loaded.  You need to be careful here, there is a potential for confusion since if they are changed then they should be detected by the server and cachine settings as needing to be re-loaded.

It would be so nice if they did work, in our corporate software we jump through hoops to get over this problem, and we use the randome method described above.

Author Comment

ID: 1213810
..thank you all so much.

   Content-type: text/html\nCache Control: no-cache\n\n

works perfectly.  I didn't actually test the other method since I resolved my problem using the line above, it seems ok even with ie. 4.0 and NetN. 3.0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question