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

Hi,
Here's the picture:
I have a html form which accepts some data in a text box, etc. and passes it on to a perl - cgi script upon submit. The perl script manipulates the data, saves it into a text file, and gives a file report based on the contents of the text file. The cgi comes up something like this:

http://localhost/cgi-bin/sample_report.pl?value=xyz.

The problem is that after the cgi-report is displayed, if the user refreshes the page with the ?value=xyz tag in it,
then the text file is also updated with the value. I considered making the cgi refresh-disabled, but if another user adds an entry somewhere else, I should be able to view the new report after refresh.
Any suggestions would help...

Thanks.
Rupz.

Replies are listed 'Best First'.
Re: perl cgi - refresh question
by pfaut (Priest) on Apr 15, 2003 at 12:02 UTC

    Separate the update and report functions into two CGI scripts. After performing the update, issue a redirect to the report script. Then, if the user hits refresh, they'll only be refreshing the report and not the data submit.

    90% of every Perl application is already written.
    dragonchild
Re: perl cgi - refresh question
by zby (Vicar) on Apr 15, 2003 at 11:52 UTC
    How about adding one more unique parameter and checking every time if this particular value of that parameter was allready used?
Re: perl cgi - refresh question
by jonnyfolk (Vicar) on Apr 15, 2003 at 12:15 UTC
    One thing you could do would be to include an identity variable to activate a subroutine/script which updates the text file, without it it simply reads it:
    my $check=param('check'); my $value=param('value'); if (($value) && ($check)) { #update text file here or go to sub routine } else { #show report }
    Or something like that...

    Update: The check variable would be included in the form submission, and the above code has been revised to reflect that. On discussion with zby this would not work if it is desirable to update the file via the URL - I have assumed it is not. If updates from the URL are required then zby's suggestion would allow this.

      The problem is that when the page is refreshed than the identity variable will be resent.
        No, because the URL still stands:
        http://localhost/cgi-bin/sample_report.pl?value=xyz.

        the $check variable is used in the script to differentiate between viewing (which is what appears to be needed here) and editing.

        If the idea is that the person can update by clicking the URL the $check can be included in the param() call and added by the user (who presumably is in the loop) to show that the file should be updated:

        http://localhost/cgi-bin/sample_report.pl?value=xyz&check=1

        Update:
        (N.B. the &check=1 is added by the user, not received from the script, and, of course, one should think about adding a password for good measure...)
Re: perl cgi - refresh question
by kiat (Vicar) on Apr 15, 2003 at 15:04 UTC
    You might want to try this...

    When the form is submitted, send a cookie to the client's browser. So when the page is refreshed, have your code check for the presence of the cookie. If the cookie is present, it means the entry has already been added so have the script print a message such as "Your submisstion has already been processed".

    What if the user has disabled cookies? In this case, when the form is accessed, set a cookie to the client's browser. When the form is submitted, check for the presence of this form-accessed cookie. If no cookie is present, it means cookies have been disabled and so the submission will not be processed. In other words, have the script check for the presence of cookies before allowing further processing.
Re: perl cgi - refresh question
by empeka (Initiate) on Apr 15, 2003 at 16:41 UTC
    I'm a bit hazy on HTML META tags, but the Refresh tag can have a target (and defaults to itself if none is set.)

    With the CGI module use,

    my $location = $query->url();

    Which should return the full URL without state information (e.g. query parameters) and set the Refresh: target to $location.

    --
    Fundamentally, there may be no basis for anything.

Re: perl cgi - refresh question
by Anonymous Monk on Apr 15, 2003 at 17:04 UTC
    Can you check the REFERER environment variable? I would guess that if the REFERER is the form, go ahead and update the text file, but if the REFERER is blank (refresh), don't update the text file.
      The referrer is a notoriously unreliable source of information. Many clients don't send one, sometimes it's filtered out by proxies, occasionally it's even faked up from other criteria (like the requested URL). It's user-supplied data, and as such shouldn't be trusted any more than any other data.

      Makeshifts last the longest.

Re: perl cgi - refresh question
by isotope (Deacon) on Apr 16, 2003 at 16:07 UTC
    This is really what POST is for -- submitted data should be POSTed, and if the user tries to reload, most browsers will ask if he really wants to resubmit the info. Provide a 'refresh this page' link to sample_report.pl if you want, following it will result in a GET request with no submission.

    --isotope
    http://www.skylab.org/~isotope/
Re: perl cgi - refresh question
by rupesh (Hermit) on Apr 16, 2003 at 04:19 UTC
    Thanks to all you people....
    I didn't expect such a great response... Man!! what a site!... Anyhow, as per your suggestions, i'm going to try it one by one...
    I'll let you guys know once i've tested each one of 'em.
    Once again... THanks to all!!