Return to the guestbook discussion
Subject: Re: Perl tutorial -- guestbook example Date: Thu, 20 Jan 2000 09:51:27 -0800 From: John CallenderTo: [name deleted] [name deleted] wrote: > > Hello, > > I have just started learnign Perl online. And I > copied your guestbook.cgi for my guest book. The > problem is that after I add a comment to the > guestbook, if I reload the page, my comment gets added > once again, and, in fact, the comment gets re-added as > many times as many times I reload the page. I think > that the reason is that, you have a condition that > checks whether the submit button has been clicked, and > in that case, the guestbook gets updated. And, > really, after you submit your comment, the button is > clicked, and the condition is satisfied all the time > since then. So, I think, I should add a line that > would, after I click the submit button and the > guestbook.txt is updated, change the action back to no > 'action' again. Since I am so new to cgi (this is my > second day), I don't really know how this can be done. > Could you help please? When you "reload" a page that was shown to you as the result of submitting an HTML form with method "POST", your Web browser is actually re-submitting the form. In the HTTP spec, "POST" transactions are different from "GET" transactions, in the sense that a "GET" transaction is supposed to be locally cache-able and bookmark-able; that is, it's always supposed to return more or less the same thing, or at least, submitting a GET request multiple times is not supposed to change anything on the server side. POST transactions, though, are specifically intended for transactions that may very well change something on the server side. Since that's the case, re-submitting a form via method POST (or hitting the Reload or Refresh button in your browser when viewing a page that was created via a POST request), is not guaranteed to deliver you the same page as the one you were looking at before you reloaded. I guess what I'm trying to say is that what you're describing is actually a feature of how the Web works, rather than being a bug. That said, this issue has come up before, when someone wanted a way to prevent the situation where a user double-clicks (instead of single clicking) on the guestbook form's submit button. The way it's written right now, doing that will cause duplicate entries to appear in the guestbook, just like it will when you do the reload thing. I didn't write the guestbook script to do anything fancy to prevent that, in part because I wanted it to be as simple as possible for learning purposes. But it wouldn't be too hard to extend it to be smart enough not to post a duplicate entry. Here's one way you could do that: Every time the guestbook script delivers a form for the user to look at, and possibly submit, it generates a unique ID number and embeds it in the form in a hidden form field. Then, when it writes an entry to the guestbook, the entry includes an HTML comment that contains that unique ID. When the guestbook script processes a form submission, it first checks to see if an entry with that ID is already in the guestbook. If it is, the script doesn't do anything, but just shows the guestbook in its current form. Only if the ID accompanying the current submission is *not* in the guestbook would the script actually add an entry. Generating a unique ID is fairly easy; you could do something like: $id = $$ . time; # current process ID, concatenated with Unix # seconds Checking for the current submission's ID in the existing guestbook entries could be done quickly with a regular expression. I don't have time right now to enhance the guestbook in this way, but if you take that on, and come up with a version that works, I'd be interested in taking a look at it. Good luck! I hope you find the tutorial useful! John