Here is a program placed on your server that can be called by multiple forms at the same time with locking. I show it in my Perl coding class as an example of how to capture information from visitors that fill out a form. It has not gone through any exaustive testing but can give you some pointers. It is not fancy, is well documented and simple. It writes each form field as a serarate text record with "Field_name = Field_value".
#!/usr/bin/perl
#****************************************************************************
# THIS PROGRAM READS STANDARD INPUT AND WRITES .HTM CODE BACK TO THE CLIENT *
# USING STANDARD OUTPUT TO BE DISPLAYED BY THE BROWSER. *
# IT ALSO WRITES A 'FORM's FIELD-NAMES & VALUES TO A DATA FILE. *
# THE PROGRAM IS SAVED AS "CIM355_WRITE_FORM_DATA_TO_FILE.PL" *
#****************************************************************************
#****************************************************************************
# THE FOLLOWING TWO STATEMENTS INSTRUCT THE PERL INTERPRETER TO LINK MODULES*
# FROM THE FOLLOWING TWO SUBROUTINE LIBRARIES *
#****************************************************************************
use CGI;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser); #RETURN ERRROR CODES TO THE BROWSER
#****************************************************************************
# THE FOLLOWING LINE IDENTIFIES THE DESIRED FILE NAME FOR THE OUTPUT FILE. *
#****************************************************************************
$OUTFILE="CIM355_WRITE_FORM_DATA_TO_FILE.TXT";
#****************************************************************************
# THE FOLLOWING STATEMENT MUST BE SENT WITH THE DOUBLE LINE FEED TO *
# INFORM THE BROWSER THAT THE RETURNING DATA IS IN THE FORM OF A *
# "HTML WEB PAGE - IN TEXT FORMAT". *
#****************************************************************************
print "Content-type: text/html\n\n";
#****************************************************************************
# THE OTHER TWO LINES ARE TO DEFINE THE STANDARD HEADER & BODY OF A WEB PAGE*
#****************************************************************************
print "<HTML>\n<HEAD>\n<TITLE>FORM DATA HAS BEEN PROCESSED.</TITLE>\n</HEAD>\n";
print "<BODY><PRE><BR>\n";
#****************************************************************************
# NOW Lets open a LOCK file to protect against concurrent user update. *
#****************************************************************************
flock(LOCKFILE, "FILELOCK.TXT"
#****************************************************************************
# NOW Lets open an OUTPUT or APPEND to a disk text file. *
#****************************************************************************
$append = "yes";
if (open(FORM_REC_OUT, ">>$OUTFILE")
{
print "APPENDED OUTPUT FILE ON SERVER WAS OPENED\n\n\n";
}
else
{
print "Cannot open $OUTFILE! FOR APPEND,\n";
print "Will try to OPEN for OUTPUT.\n\n";
$append = "no";
}
#----------------------------------------------------------------------------
if ($append eq "no"
{
if (open(FORM_REC_OUT, ">$OUTFILE")
{
print "OUTPUT FILE WAS OPENED AS A NEW FILE ON THE SERVER.\n\n\n";
}
else
{
print "Cannot open $OUTFILE FILE IN ANY FORMAT!\n\n";
exit 99;
}
}
#****************************************************************************
# SEND AN ACKNOWLEDGEMENT BACK TO THE CLIENT IN THE FORM OF A WEB PAGE. *
#****************************************************************************
print "<CENTER><h2>";
print "YOUR FORM's DATA HAS BEEN WRITTEN TO<BR>$OUTFILE<BR>ON THE SERVER.\n";
print "-----------------------------------------------------------\n";
print "</CENTER></h2><br>";
#****************************************************************************
# LOOP THROUGH THE STANDARD INPUT(PIPED) STREAM AND WRITE EACH INPUT FIELD *
# IN THE FORM TO A SERARATE LINE IN THE OUTPUT FILE WITH IT's NAME. *
#****************************************************************************
#print "***",scalar(localtime),"***\n";
($sec, $min, $hour, $mday, $mon, $year_off, $wday,$yday,$isdst)=localtime;
print FORM_REC_OUT ($mon+1,"/",$mday,"/",$year_off+1900," TIME ",$hour,":",$min,":",$sec,"\n"
while(defined($a = <STDIN>))
{
$a =~ tr/&/\n/;
$L = length($a);
print FORM_REC_OUT '=>',$L,'<=>',$a,'<=',"\n"; # Debug lines
$e = 0;
for ($i = 0; $i < $L; $i++)
{
if (substr($a,$i,1) eq '=')
{
$e = $i + 1;
}
}
print FORM_REC_OUT '=>',$e,'<=',"\n"; # Debug lines
# $a =~ tr/&/\n/;
print FORM_REC_OUT $a,"\n";
}
#****************************************************************************
# COMPLETE THE ACKNOWLEDGEMENT WEB PAGE BACK TO THE CLIENT SYSTEM. *
#****************************************************************************
print FORM_REC_OUT "-------------------------------\n";
print "<BR>\n";
print "FORM DATA WAS WRITTEN TO OUTPUT FILE.\n";
print "</PRE></BODY></HTML>\n";
#****************************************************************************
# CLOSE THE DATA FILE AND THE LOCKING FILE. *
#****************************************************************************
close FORM_REC_OUT;
close LOCKFILE;
exit 0;
#****************************************************************************
Sorry, as a one finger data entry person I often leave out small details. I have a 1 byte text file I created that I never read/write to. I just use it as a file to lock against to serialize the operations against the text file I append to.
I also have another program I can run at any time and it locks the byte file then reads ->writes the current data to a work from file then resets the append file for more accumulations. I then download the collected data to my local client system for Cobol processing.
All in all, there are a group of files that do the whole job. I was just trying to show the basic code of txt file writing and include the concern about concurrent clients going against the server at the same time, not the whole solution.
Like I said, no exhaustive testing. Our simple test was, we got a class of users(6-7) to each bring up the form page and fill out a set of values then press the enter key at the same time. We did not have any interspersed form data.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.