Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Why does my script run again, when I refresh my results page?

( #19694=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 24, 2000 at 05:55 UTC
Q&A  > CGI programming


Description:

I have a script that sends a form with data, checks for a string to be in a file at the server, and returns a file which is updated each time the script is run; but once I run the script and got the results, if I reload the results page, the script runs again, and my last sent data adds again (it's a table, and I add a row each time the script is ran).

How can I avoid this...????

Answer: Why does my script run again, when I refresh my results page?
contributed by iguanodon

One way I've handled this is to use a session management framework and put something in the session on the first submit to indicate that it has been processed. I check for the presence of this flag before doing anything with the form data and show an appropriate message if I see that it's already been done.

I use Apache::Session but there are other options.

Answer: Why does my script run again, when I refresh my results page?
contributed by Corion

The problem is the same as it is here with the chatterbox, if a page is refreshed or submitted twice, a double post will occur.

A very elegant solution was proposed for this problem by swiftone. The idea was to include with every page a unique ID (unique for 1 hour or so) and that the script would keep a list of which unique IDs had submitted a chat line. If a page was reloaded, that would show up as a double ID and the script could then determine what to do (serve the old results, process the new/old/resubmitted query etc.).

Answer: Why does my script run again, when I refresh my results page?
contributed by pemungkah

You can force a browser to think you're accessing a new URL (and thereby force it into the history) by adding a dummy "uniquifier" to the URL. As long as the URL "looks different" to the browser (i.e., it's not character-for-character identical), the browser will treat it as a "different page". Pages with uniquifiers get added to the history, and can be back-buttoned to.

Your server can completely ignore the uniquifier, as long as it sends a new one every time.Try something like this: http://myserver.com/cgi-bin/myscript.cgi?u=438292& ..., and just keep changing the number. For forms, use a URL like this as the form's action: http://myserver.com/cgi-bin/myscript.cgi/u432443, which will do the same thing.

You have the disadvantage that the user can back-button to a page and then proceed "forward" again from there, generating the kind of problem you were speaking of, but you can (if you've got some kind of a session mechanism set up) remember the uniqifier from a "problem" page and skip doing the thing you don't want if the uniqifier is the one you remembered.

Answer: Why does my script run again, when I refresh my results page?
contributed by chromatic

If you reload a page, you may be sending form data to the script again. Your script could check for duplicate data, or you could close your browser and open the results page from scratch. You could also follow a bookmark to the results page, as long as the bookmark didn't include any query string data.

Answer: Why does my script run again, when I refresh my results page?
contributed by Anonymous Monk

i don't want to confuse matters but i've come up against this one a couple of times and yet to find an easy, safe cure - what i'd like to do is wipe the browser history and replace the url with a new one. i think there's some functions in javascript to do this but i've not yet got it to work. Is there any way of forcing a new url into the browser history with perl?

Answer: Why does my script run again, when I refresh my results page?
contributed by QandAEditors

Yes. That is a problem. Reloading the script is equivalent to re-running it. The reason why the "results" page won't display is because it is not an "actual page" but rather just generated by the CGI program. As some of the posts here have suggetsed, the best way to solve it is to prevent the data from being re-entered. You could do this by 1) having your script check for duplicate data or 2) maybee write a cookie to their side that lets your script know whether this person has "submitted" before.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (5)
    As of 2014-09-24 05:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (246 votes), past polls