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

This script I wrote writes a random string as an image and asks the user to type in the digits. I have that down, no problems there. And for variety I added a bunch of offsets for randomity (if infact it's a word :)), so I feel quite proud because I think it loads rather nice. I do have a problem with it though. If you guess the image right or wrong, it will NOT reload the image but it will rewrite a new random $ID. So if the image was 123456789 and you guessed 123456780, it will tell you you're wrong, make a new ID but the image will still print the original string. Can someone point out why this is happening and how I can fix it?
#!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; use Image::Magick; print header, start_html('testing'); if (param('submit')) { my $code = param('code'); my $id = param('id'); print "ID: $id<p>"; if($code eq "$id") { print "You entered the correct code, well done<p><p>";} else { print "<font color=red>You have entered the wrong code. Please try ag +ain</font><p><p>"; } } my ($image, $x); $image=Image::Magick->new; $x = $image->Set(size=>'200x50'); $x = $image->ReadImage('xc:white'); warn "$x" if "$x"; my $chars; my @fonts = ("arial.ttf", "georgia.ttf", "verdanai.ttf"); my @chars = ( "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", " +N", "P" .. "Z", "a", "b", "d", "e", "n", "q", "r", 1 .. 9, ); my @yoffset = ("25", "28", "33", "35", "38", "41", "45"); my @pointsoffseth = ("5,30 199,30", "5,35 199,35", "5,40 199,40", "5,2 +4 199,24", "5,20 199,20", "5,42 199, 42", "5,50 199,50", "5, 55 199, 55"); my @pointsoffsetv = ("100,50 100,0", "90,50 90,0", "110,50 110,0", "85 +,50 85,0", "75,50 75,0", "65,50 65,0", "60,50 60,0", "50,50, 50,0", "40,50, 40,0", "25,50 25,0", "20,5 +0 20,0", "15,50 15,0", "10,50 10,0", "5,50 5,0", "115,50 115,0", "120,50 120,0", "125,50 125,0", "130,50 130,0", " +135,50 135,0", "140,50 140,0", "145,50 145,0", "150,50 150,50", "155,50 155,0", "160,50 160,50", "165,50 165,50", + "170,50 170,50", "175,50 175,50", "180,50 180,50", "185,50 185,0", "190,50 190,0", "195,50 195,0", " +199,50 199,0"); my $ID; ### randomize characters $ID = join '', map { $chars[ rand @chars ] } 1..9; ## length -- > by height ^ for (1 .. 3) { my $pointsoffseth = $pointsoffseth[rand @pointsoffseth]; ## Draw horizontal lines $x = $image->Draw( primitive => 'line', points => $pointsoffseth, stroke => 'black', ); } ## Draw verticle lines for (1 .. 6) { my $pointsoffsetv = $pointsoffsetv[rand @pointsoffsetv]; $x = $image->Draw( primitive => 'line', points => $pointsoffsetv, stroke => '#800', ); } my $xoffset = 10; foreach (split //, $ID) { my $yoffset = $yoffset[rand @yoffset]; my $font = $fonts[rand @fonts]; #print "Font: $font"; $x = $image->Annotate( font=>$font, pointsize=>'26', fill=>'blue', text=>$_, x=>$xoffset, y=>$yoffset); $xoffset = $xoffset + 21; warn "$x" if "$x"; } $x = $image->Write('test.png'); warn "$x" if "$x"; print "Image testing<br>"; print "<center><img src=\"test.png\"></center>"; print <<"ALL"; <center> <form action="random.pl" method="POST"> <input type="text" name="code"> <input type="hidden" name="id" value="$ID"> <input type="submit" value="submit" name="submit"> </form> </center> ALL


"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

Replies are listed 'Best First'.
Re: Item not refreshing with the page
by atcroft (Abbot) on Jan 25, 2004 at 03:22 UTC

    From what you describe, my first guess is that the browser may be caching the image. You may wish to look at having the page you generate attempt to force the browser not to catch it or its components. A simple test would be to try this, then clear the browser's cache and see if it does it when you reload the page. Just a thought, but thought maybe it might help to check into.

Re: Item not refreshing with the page
by jdtoronto (Prior) on Jan 25, 2004 at 03:52 UTC
    I think atcroft is right. If the image name/size does not change then the browser will use its owned cached version. Try adding a 'no-cache' pragma to your HTML header. I note you are using CGI.pm, therefore the additional header is:
    print header( -pragma => 'no-cache' );
    This should handle it with any luck.

    jdtoronto

      I've seen images refuse to refresh even when the rest of the page was. If this still happens, try somethng like:

      $dummy = rand(1000); print "<center><img src=\"test.png?x=$dummy\"></center>";
      to convince the browser (and any intermediate caching things) that the image is in fact query.

      --Bob Niederman, http://bob-n.com

      All code given here is UNTESTED unless otherwise stated.

        Your code is engenius, it really is. That totally fools the browser into thinking it's something it can't cache and it worked. That's utterly amazing, thank you for this tip!

        And thank you to the others. I didn't know an image will stay in cache as long as the name and size doesn't change. I just thought it reloaded the image if it realizes the page has been updated since the last time it's visited. Thanks for that information.



        "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