CGI form repost from browser--how to prevent?

Suchandra Thapa ssthapa at classes.cs.uchicago.edu
Wed Dec 19 21:59:30 EST 2001


Chris <bit_bucket5 at hotmail.com> wrote:
>Thanks to all for input.  Unfortunately, the Javascript solution is
>not working for me.  I think maybe I did not state the issue clearly
>enough, or maybe my meager Javascript knowledge is a problem.  The
>issue is not preventing a submit while a current sumbit is processing.
> The issue is preventing a browser refresh (F5 in IE, eg) causing a
>completely new submit after the user has hit the submit button and
>that submit processing is complete.  I.e., here's the scenario:
>User enters data into form.
>User hits submit button.
>Cgi script invoked and does submit work.
>Cgi script returns, user presented with new page stating "submit
>done--all ok!"
>User hits refresh in the browser (or "reload").
>Browser reposts the form data.
>Cgi script runs anew and repeats the same work it just did.
>
>That last step is what I want to be able to prevent.  I tried the
>Javascript and it doesn't prevent that scenario.  At least *my*
>Javascript doesn't prevent that scenario :-).


    Try this instead. Immediately after doing your processing, redirect
the browser to another page.  You can do this by sending a location
header with the new page (e.g. "Location: http://new.address.com").  This
prevents a refresh from resubmitting the parameters.  For protection against
the user going back using the history, set a cookie with a value like
processed=myform.py by the script that does the processing.  And have any 
scripts that generate forms reset this value.  That way, if your form 
processing script sees this value it'll know that the user came back to the
page using the browser's history.  
    Another thing you can try is to use get form actions since those will 
be cached.  This won't work for forms with a large amount of variables or
things like file uploads, but it also doesn't need cookies to be enabled.

-- 
----------------------------------------------------------------------------
	   		              |
Suchandra Thapa                       | "There are only two kinds of math . 
s-thapa-11 at NOSPAMalumni.uchicago.edu  | books. Those you cannot read beyond 
			              | the first sentence, and those you 
			              | can not read beyond the first page."
			              |                     -C.N. Yang
----------------------------------------------------------------------------



More information about the Python-list mailing list