page/form time out? pts for quickness

Posted on 1998-07-31
Last Modified: 2010-08-05

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.
Question by:generous
  • 5
  • 3
  • 3
  • +2

Expert Comment

ID: 1208940
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?


Expert Comment

ID: 1208941
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">


Author Comment

ID: 1208942
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.

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 84

Expert Comment

ID: 1208943
Wouldn't it be easier to relocate the waiting users to another page?

Author Comment

ID: 1208944
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.

Expert Comment

ID: 1208945
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).


Expert Comment

ID: 1208946
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!

Author Comment

ID: 1208947
I would love the code!!!!!
thank you.

Accepted Solution

alamo earned 130 total points
ID: 1208948
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;


Author Comment

ID: 1208949
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.


Expert Comment

ID: 1208950
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 :)

Expert Comment

ID: 1208951

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";

Author Comment

ID: 1208952
Thanks, your answer was great.  I have learned a lot, thanks.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pattern matching in perl 2 105
Insert Text into odbc.ini file 15 67
Using Perl DBI to query oracle 3 46
Perl tar error 8 66
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question