page/form time out? pts for quickness


I need to limit the amount of time a users has to submit information from a form on a webpage.  The users should have 5 mins to submit their info and after that script must locate them to another page.  If it is possible, the info already inputted should be transferred through the submit button.  

I don't have a clue how to do this or how hard it is, so i hope the pts are accurate.  If not let me know and i will bump it up.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

alamoConnect With a Mentor Commented:
Here's a simple example perl CGI script that locks and times out a lock on a page. There's even some javascript to tell the user how much longer they have on the status line.

Load it in two windows to see how the lock works. You need to create a file lock.txt for this to work.

$locksecs = 30;       # lock timeout, set to 300 for 5 minutes
$extrasecs = 5;       # extra seconds to give user, to account for page loading time

print "Content-type: text/html\n\n";
&ReadParse;           # read GET arguments

# open, filelock, and read data file
open(LOCK, "+<lock.txt") || die "Can't open lock.txt: $!";
$LOCK_EX = 2; # obtain exclusive file lock
do {} until (flock(LOCK,$LOCK_EX));
chomp ($lockdata = <LOCK>);
($lastlockcode,$lastlocktime) = split(/:/, $lockdata);

# if we have a lock code, then it's a user trying to update their record
if ($FORM{'lockcode'})
   # Ok to update if lock codes match (noone else has locked page since)
   if ($FORM{'lockcode'} == $lastlockcode)
      print "<head><title>Update Accepted</title></head>\n";
      print "<body>Record Updated!</body>\n";

      print LOCK $lastlockcode+1,":0\n";      # release user's lock on the page
      close LOCK;
      print "<head><title>Update Rejected</title></head>\n";
      print "<body>Sorry, you waited too long and someone else has locked this page.</body>\n";
   # user wants to lock a record. see if someone else has it locked already
   if (time - $lastlocktime > $locksecs+$extrasecs)
      print LOCK ++$lastlockcode,":",time,"\n";
      close (LOCK) || die;

      print "<head><title>Update Form</title>";
      print "<script>\n";
      print "function timemessage(secs) {\n";
      print "  if (secs == 0) { window.status = 'Time Expired, submit now!'; }\n";
      print "  else { window.status = 'You have ' + secs + ' seconds to complete this form';\n";
      print "         window.setTimeout('timemessage('+(secs-1)+')',1000);}\n";
      print "}\n";
      print "timemessage($locksecs);\n";
      print "</script></head>\n";
      print "<body>You have $locksecs seconds after this page loads to submit this form.<p>\n";
      print "<form method=GET>\n";
      print "<input type=hidden name=lockcode value=$lastlockcode>\n";
      print "Enter something: <input type=text name=something>\n";
      print "<input type=submit value=\"Submit\">\n";
      print "</form></body>\n";
      print "<head><title>Already Locked</title></head>\n";
      print "<body>Sorry, someone has this record locked already. Please reload this page in ", $lastlocktime+$locksecs+$extrasecs+1-time," seconds</body>\n";

sub ReadParse {
  local(@rawargs) = split(/&/, $ENV{'QUERY_STRING'});
  foreach (@rawargs) {
    local($name, $value) = split(/=/);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;
    $FORM{$name} = $value;

Using JavaScript you could do the timer-thing like this:
<BODY ... OnLoad="timerId=setTimeout('self.location.href=\'goto.htm\'', 5*60000);">
This will set a timer for 5 minutes. Then the browser will be redirected to goto.htm.

<FORM ... OnSubmit="clearTimeout(timerId)">
When the form is submitted the timer is removed.

I don't really get what you mean with
"If it is possible, the info already inputted should be transferred through the submit button."

Could you please explain that part for me?

Wait a minute!

Do you mean that when the time is up the form should be auto-sent and the user should be forwarded to another page?

If so, here's the new onLoad code:
OnLoad="theForm.submit(); timerId=setTimeout('self.location.href=\'goto.htm\'', 5*60000);"

You must also name the form:
<FORM ... NAME="theForm">

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

generousAuthor Commented:
See i have a form with info in it that must be updated by users, except that it is locked to allow only one person in at a time.  Now i don't want one person to take 20 mins filling it out while others wait.  So the code must:  1)time the user out and 2) either relocate them to another page Or retain the info in the form by pressing the submit button.  I was thinking of perl code b/c it will allow for removal (using the system rm cmd) of a locking file (thus allowing other to access the page).  I hope this helps.
I can piece this javascript code together to work with some perl, so if you can't think of something else, the pts are yours.

Wouldn't it be easier to relocate the waiting users to another page?
generousAuthor Commented:
I don't know if that would be possible, but i doubt it do to the way the perl is written.  I am going to test the javascript after my late lunch.  If it works the pts are yours.  Thanks for the quick responses.

Will this
OnLoad="theForm.submit(); timerId=setTimeout('self.location.href=\'goto.htm\'',
both submit the info and redirect the user?  If it will then the points are yours.
Ooops. Major bug!
Correct onLoad:
"timerId=setTimeout('theForm.submit();self.location.href=\'goto.htm\';', 5*60000);"

Actually, I am not sure if this will work properly...
Maybe that that the posting will be interrupted by the redirect statement.
If it does, I have a solution (nested timers).

Javascript is a decent way to warn your user that their time is limited, and maybe take them to anothe page, but it's a bad way to do this timeout (it won't work)

The timeout has to be implemented on the server-side:

- When someone locks the page, the time is recorded and a hidden field with a lock code is placed into the form.

- The next time someone tries to lock it, the current time is compared to the locked time. If it's greater than five minutes old then the old user's lock is removed and the new user gets the locked page, otherwise they are told they must wait.

- If the old user submits after 5 minutes and they no longer have the lock (which you can tell from the hidden lock code field), you can tell them that their update was ignored since someone else is updating the page. (If noone else has locked the page in the meantime, there's no reason not to allow the update).

That's the sort of system required, javascript alone can't do it. Tell me if you want an example. Hope this helps!
generousAuthor Commented:
I would love the code!!!!!
thank you.
generousAuthor Commented:
Umm, I am going to need some time with this one. I was just creating a tmp, if the file existed then the person got a msg saying to try again. Your code (from the parts that i can understand)  seems to do a lot more and i will try to use it.

I don't understand how this code kicks the user out / sends the info automatically after x secs.  Could you just highlight that code?  Sorry for being so difficult.

It is the long weekend and my girlfriend will kill me if i take the laptop camping, so pls give me a few days (tues night).


ps thanks for the frame ideas too.

Alamo, that's a nice piece.  It amazes me sometimes how few of the questions in this topic have much at all to do with perl :)

The code as is doesn't actually submit the page after the timeout, it just puts "Time Expired, submit now!' on the status line.

As I am interpreting your reason for  wanting to autosubmit, it's because your page locking doesn't work properly, it has holes. (Please forgive me if I am wrong on this, I am reading between the lines here). The above script closes the holes, making the automatic submission unnecessary. The timeout is done entirely on the server, depending on action from the client won't work because there are a million reasons that action may not happen. As it is written, the script tells the user they better submit, but will still accept their update after the timeout *as long as* noone else has yet locked the page. If someone else has locked the page in the interim, then they get a message telling them their update was rejected. If someone tries to update a page they haven't locked (for example if the page was in their cache) their update will be rejected. If the client loads the page and then goes away forever, the page lock will still time out properly.

By the way, creating a file unless it already exists is unreliable as a lock mechanism, though admittedly in your situation I wouldn't expect it to fail often. The way the script does it is reliable, though, by locking an existing file for exclusive access and then writing the page lock information to it.

If you really want to auto-submit after the timeout, change the line
 print "  if (secs == 0) { window.status = 'Time Expired, submit now!'; }\n";
 print "  if (secs == 0) { document.forms[0].submit(); }\n";
generousAuthor Commented:
Thanks, your answer was great.  I have learned a lot, thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.