http://www.perlmonks.org?node_id=19694

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

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...????

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Why does my script run again, when I refresh my results page?
by iguanodon (Priest) on Jan 03, 2003 at 20:24 UTC
    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.

Re: Why does my script run again, when I refresh my results page?
by Corion (Patriarch) on Jun 24, 2000 at 13:30 UTC

    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.).

Re: Why does my script run again, when I refresh my results page?
by pemungkah (Priest) on Apr 09, 2003 at 19:59 UTC
    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.

Re: Why does my script run again, when I refresh my results page?
by Anonymous Monk on Jan 03, 2003 at 15:42 UTC
    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?
Re: Why does my script run again, when I refresh my results page?
by chromatic (Archbishop) on Jun 24, 2000 at 06:01 UTC
    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.
Re: Why does my script run again, when I refresh my results page?
by Anonymous Monk on Jun 28, 2000 at 06:31 UTC
    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.