pdl_babu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am creating a html page dynamically using cgi, perl. Based on the date range selected I create a production report and a chart (a .jpg image). I display the .jpg image using the <img> tag.

The problem is whenever the user goes back and selects a different date the old chart is getting displayed instead of the new one created.

I found that the .jpg at the users "Temporary Internet" file is the problem.

Pls. help me on how to overcome this problem.

Thanks in advance

Pdl_babu

Replies are listed 'Best First'.
Re: image in <img> tag is not refreshed
by BrowserUk (Patriarch) on Nov 12, 2005 at 15:45 UTC

    Are you using the same name for the .jpg file each time you create a new one? If so, the browser is probably not asking the server for the new one because it thinks it already has it in it's cache. Use a new name each time you create a new jpg.

    The md5 of the .jpg might make a good choice, or use File::Temp.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: image in <img> tag is not refreshed
by nobull (Friar) on Nov 12, 2005 at 15:46 UTC
    Your problem has nothing to do with Perl.

    Use a different file name for your .jpg each time. This not only solves your problem but also the race condition that exists if two users are accessing your site at the same time.

    It does create a need to garbage collect your tempory image directory from time to time.

    Alternatively make the <img> tag point to another CGI script that creates the JPEG entity on-the-fly and never saves it as a file.

    Did I mention that this has nothing to do with Perl?

Re: image in <img> tag is not refreshed
by Tanktalus (Canon) on Nov 12, 2005 at 17:41 UTC

    Alternately, don't produce the chart at all - have that done in another runmode (using CGI::Application terms) or another CGI script or whatever. And then set the cache time to 0 in your headers before printing it out. This prevents the race condition mentioned above, eliminates the client's tendency to cache it, and probably allows you to use the server CPU time more effectively (since these will be two processes rather than one), and also start sending back the HTML sooner (since producing the chart won't completely stop producing the HTML).

    CGI scripts can send back images just fine - you just have to manipulate the header appropriately.

Re: image in <img> tag is not refreshed
by davidrw (Prior) on Nov 12, 2005 at 15:44 UTC
    Can you show the code that generates the <img> tag? Is it always <img src="yourreport.jpg"> or is it like <img src="report20050105-20050120.jpg">? If the first one, then it's probably a caching issue .. if the second, probably an issue with your chart generation (maybe not passing in the new date range properly or something like that, but just guessing w/o seeing code)
Re: image in <img> tag is not refreshed
by sulfericacid (Deacon) on Nov 13, 2005 at 03:19 UTC
    Hi.

    An easy solution would be to use a three digit random number and append it to the image.

    <img src="image.jpg?123"> <img src="image.jpg?526"> <img src="image.jpg?083">
    It'll load the image each time. I used this fix a while back. Hope it helps.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: image in <img> tag is not refreshed
by slayven (Pilgrim) on Nov 13, 2005 at 15:14 UTC

    Like in the previous contributions I'm asuming that you're not changing the image filename so when the browser reloads it takes the image from its cache ignoring the changes.

    Now, to prevent the browser to cache your images just tell him to do it. Put the following Meta-Tag into your HEAD section of your HTML code.

    <head> <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE" /> </head>

    -- slayven
            trust in bash
            but tie your camel
    

      that is html, and only affects the html file, not the image
        Hi,

        I just recently had a similar problem. To solve it, I added a .htaccess file to the directory with the images with the following content:

        ### activate mod_expires ExpiresActive On ### Expire .gif's immediately (0) after they have been accessed (A) ExpiresByType image/gif A0 ### or set "ExpiresDefault" for all files in the directory
        more on that here: http://www.apacheref.com/ref/mod_expires.html
        see ye,
        svenXY (currently not logged in)