noree97
asked on
validating ID
I tried running the code below to check id the ID entered is correct but nothing seems to happen. What is wrong?
----------------------
content of stud_id.txt
a1234567 aaaaaa
b1234567 bbbbbb
c1234567 cccccc
d1234567 dddddd
e1234567 eeeeee
f1234567 ffffff
-------------------
#!/apps/perl5/bin/perl
#Change this path!
$DATA_FILE = "/iws/s2389945/hci3.dat";
# The associative array $values will contain the data.
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
# Make sure it's a POST-method form submission
if (! &VerifyForm()) {
print "<title>Not a POST Form Submission</title>\n";
print "</head>\n";
print "<h1>Not a POST Form Submission</h1>\n";
print "This page should be accessed only by submitting\n";
print "a form using the POST method. Perhaps your\n";
print "browser does not support forms.\n";
print "</body></html>\n";
exit 0;
}
#OK, it's a form submission, so parse it.
&ParseForm();
#Use the information
#If any field is missing, complain!
if ((! $values{"studId"}) ||
(! $values{"Practical"}))
{
print "<title>Please fill out all the fields</title>\n";
print "</head>\n";
print "<h1>Please fill out all the fields</h1>\n";
print "Please fill out the name, email address, AND\n";
print "comment fields. Back up to the previous page\n";
print "to try again.\n";
print "</body></html>\n";
exit 0;
}
#------------------------- ---------- ---------- ----
# TESTING
# comparison of login id
$data="stud_id.txt";
open(DATA,"$data") || die "open failed: $!\n";
while (<DATA>) {
chop;
($id, $name) = split(/ /,$_,2);
if ($id =~ /^$values{"studId"}/)
{
print "<title>Thank you user, ", $values{"studId"}, "</title>\n";
print "</head>\n";
print "<h1>Thank you, ", $name, "</h1>\n";
print "You have registered for practical", $values{"Practical"},"\n";
print "</body></html>\n";
}
else
{
print "<title>Wrong Entry </title>\n";
print "</head>\n";
print "<h1>You gave entered an invalid ID </h1>\n";
print "Please try again \n";
print "</body></html>\n";
}
}
#------------------------- ---------- ---------- ----
#OK, we have all the data. Write it to a file in which
#we collect comments from users. Open to append, of course.
$fname = ">>" . $DATA_FILE;
open(OUT, $fname);
#The format I write here just happens to be appropriate for
#reading with a typical Unix mail reader; for instance,
#mail -f comments.txt should open the file.
#No subject lines, though.
print OUT "From: ", $values{"studId"}, " <", $values{"Practical"}, ">\n";
close(OUT);
print "<title>Thank you user, ", $values{"studId"}, "</title>\n";
print "</head>\n";
print "<h1>Thank you, ", $values{"name"}, "</h1>\n";
print "Thank you for your comments.\n";
print "</body></html>\n";
exit 0;
sub VerifyForm
{
local($bad, $contentType, $requestMethod, $result);
$bad = 0;
# Check the content type of the data we've received
$contentType = $ENV{"CONTENT_TYPE"};
if ($contentType ne "application/x-www-form-ur lencoded") {
$bad = 1;
}
# And make sure the POST method was used
$requestMethod = $ENV{"REQUEST_METHOD"};
if ($requestMethod ne "POST") {
$bad = 1;
}
$result = ! $bad;
}
sub ParseForm
{
local($fields, $name, $value, $data);
#Split standard input into fields
read(STDIN, $data, $ENV{"CONTENT_LENGTH"});
@fields = split(/&/, $data);
#Split the fields into names and values, creating
#an associative array indexed by the names
foreach $item (@fields) {
($name, $value) = split(/=/, $item);
$name =~ s/\+/ /g;
$value =~ s/\+/ /g;
$values{$name} = $value;
}
}
-------------------------- -----
Thanks
----------------------
content of stud_id.txt
a1234567 aaaaaa
b1234567 bbbbbb
c1234567 cccccc
d1234567 dddddd
e1234567 eeeeee
f1234567 ffffff
-------------------
#!/apps/perl5/bin/perl
#Change this path!
$DATA_FILE = "/iws/s2389945/hci3.dat";
# The associative array $values will contain the data.
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
# Make sure it's a POST-method form submission
if (! &VerifyForm()) {
print "<title>Not a POST Form Submission</title>\n";
print "</head>\n";
print "<h1>Not a POST Form Submission</h1>\n";
print "This page should be accessed only by submitting\n";
print "a form using the POST method. Perhaps your\n";
print "browser does not support forms.\n";
print "</body></html>\n";
exit 0;
}
#OK, it's a form submission, so parse it.
&ParseForm();
#Use the information
#If any field is missing, complain!
if ((! $values{"studId"}) ||
(! $values{"Practical"}))
{
print "<title>Please fill out all the fields</title>\n";
print "</head>\n";
print "<h1>Please fill out all the fields</h1>\n";
print "Please fill out the name, email address, AND\n";
print "comment fields. Back up to the previous page\n";
print "to try again.\n";
print "</body></html>\n";
exit 0;
}
#-------------------------
# TESTING
# comparison of login id
$data="stud_id.txt";
open(DATA,"$data") || die "open failed: $!\n";
while (<DATA>) {
chop;
($id, $name) = split(/ /,$_,2);
if ($id =~ /^$values{"studId"}/)
{
print "<title>Thank you user, ", $values{"studId"}, "</title>\n";
print "</head>\n";
print "<h1>Thank you, ", $name, "</h1>\n";
print "You have registered for practical", $values{"Practical"},"\n";
print "</body></html>\n";
}
else
{
print "<title>Wrong Entry </title>\n";
print "</head>\n";
print "<h1>You gave entered an invalid ID </h1>\n";
print "Please try again \n";
print "</body></html>\n";
}
}
#-------------------------
#OK, we have all the data. Write it to a file in which
#we collect comments from users. Open to append, of course.
$fname = ">>" . $DATA_FILE;
open(OUT, $fname);
#The format I write here just happens to be appropriate for
#reading with a typical Unix mail reader; for instance,
#mail -f comments.txt should open the file.
#No subject lines, though.
print OUT "From: ", $values{"studId"}, " <", $values{"Practical"}, ">\n";
close(OUT);
print "<title>Thank you user, ", $values{"studId"}, "</title>\n";
print "</head>\n";
print "<h1>Thank you, ", $values{"name"}, "</h1>\n";
print "Thank you for your comments.\n";
print "</body></html>\n";
exit 0;
sub VerifyForm
{
local($bad, $contentType, $requestMethod, $result);
$bad = 0;
# Check the content type of the data we've received
$contentType = $ENV{"CONTENT_TYPE"};
if ($contentType ne "application/x-www-form-ur
$bad = 1;
}
# And make sure the POST method was used
$requestMethod = $ENV{"REQUEST_METHOD"};
if ($requestMethod ne "POST") {
$bad = 1;
}
$result = ! $bad;
}
sub ParseForm
{
local($fields, $name, $value, $data);
#Split standard input into fields
read(STDIN, $data, $ENV{"CONTENT_LENGTH"});
@fields = split(/&/, $data);
#Split the fields into names and values, creating
#an associative array indexed by the names
foreach $item (@fields) {
($name, $value) = split(/=/, $item);
$name =~ s/\+/ /g;
$value =~ s/\+/ /g;
$values{$name} = $value;
}
}
--------------------------
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.