Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm a beginner at Perl and in need of help. I wrote this script that simply takes an email address and password and adds them to a data file. It also tests if any of the fields are left empty. But even when I fill in both fields, it still gives me my generated error message. Please help, I'm dumbfounded! Here's the script:
#!/usr/bin/perl require "cgi-lib.pl"; &ReadParse; $email = $in{'visitor_email_address'}; $password = $in{'visitor_password'}; print &PrintHeader; if ($email eq "" && $password eq "") { print <<"PrintTag"; <html> <head> <title>The Soccer Message Board!</title> <title>The </title> <title>The Soccer </title> <title>The Soccer Message </title> <title>The Soccer Message Board!</title> <meta name="description" CONTENT="This is the site where soccer player +s can find coaches and coaches can find players. Also be updated abo +ut tournaments and other special events."> <LINK REL=stylesheet TYPE="text/css" HREF="soccer.css"> </head> <body bgcolor="#18D018" TEXT="black"> <br> <p align="center"><marquee behavior=alternate direction=left loop=infi +nite bgcolor=#18D018 width=85% align=right><h1>Welcome to the Soccer +Message Board!</marquee></p> <h2 align="center"><font color="#C91f16">Error!</h2> <h4 align="center">You forgot to fill in one or more of the fields bel +ow. Please try again.</font></h4> <blockquote><blockquote><font size="+1">The Soccer Message Board was s +tarted to help soccer players to find coaches and for coaches to find + players. At the home page, you can conduct a message search, or pos +t a message. You will also find this site extremely simple to naviga +te and use. If you have any questions or comments, please </font><a +href="mailto:sonsofthunder4\@juno.com">email us</a><font size="+1">, +so we can help make your visit to The Soccer Message Board more enjoy +able!</blockquote></blockquote> <blockquote><blockquote><font size="+2" color="#C91f16"><b><u>IMPORTAN +T</u> :</b></FONT> <FONT SIZE="+1">If you are a first time user, you +must input your email address in the text box below. Your email addr +ess can be any combination of letters and numbers 50 characters or le +ss. Do not use your name! Your email address will be needed to cont +inue through this site. If anyone posts a vulgar, degrading, or inap +propriate message, the email addresses will be used to punish any per +petrators.</FONT><B> THIS SITE WILL NOT RUN IF YOU DO NOT ACCEPT THE + COOKIE THAT REMEMBERS YOUR EMAIL ADDRESS.</B></blockquote></blockquo +te><br> <form action="http://soccermb.netfirms.com/cgi-bin/index1.cgi" method= +"post"> <p align="center"><table CELLSPACING="5"><TR><TD><font size="+1">Your +Email Address : <input type="text" name="visitor_email_address" value +="$email" size="20" maxlength="50"></TD><td></td></font></TD></TR><TR +><TD><font size="+1">Your Password : <input type="text" name="visitor +_password" value="$password" size="18" maxlength="20"> <input type="s +ubmit" value="Enter"></font></TD></TR> </TABLE></p> </form> <blockquote><blockquote><p align="center"><font size="+1">First time v +isitor? Just </font><a href="new.htm">click here</a><font size="+1"> +.</font></p></blockquote></blockquote> <br> <blockquote><blockquote><font size="+1">NOTE : If you do not have an e +mail address, you may create one at Yahoo! for free. Just </font><a +href="http://mail.yahoo.com/">click here!</a></blockquote></blockquot +e><br> </body> </html> PrintTag exit; } if ($email !~ /.+\@.+\..+/) { print <<"PrintTag"; <html><head><title>Error!</title> <LINK REL=stylesheet TYPE=\"text/css\" HREF=\"soccer.css\"> </head><body bgcolor="#18D018" TEXT="black"> <h1 align=\"center\"><font color=\"#C91f16\">Error!</font></h1> <blockquote><blockquote><blockquote><p align=\"center\"> Please enter your email address in this format:<br><br> email\@address.com <br><br> Please <a href=\"index.htm\">try again</a>. </blockquote></blockquote></blockquote></p> </body></html> PrintTag exit; } if (-e "lock.fil") { print <<"PrintTag"; <HTML> <HEAD> <TITLE>File in use</TITLE> </HEAD> <BODY BGCOLOR="#18D018" TEXT="black"> <H1>Please try again!</H1> <BLOCKQUOTE> The database is in use. Please try again later. </BLOCKQUOTE> </BODY> </HTML> PrintTag exit; } else { open(LOCK_FILE, ">lock.fil"); open(FILE,">>data.txt") || die "Can't find database\n"; print FILE "$email|$password\n"; close(FILE); close(LOCK_FILE); unlink("lock.fil"); print <<"PrintTag"; <html><head><title>Welcome!</title> <LINK REL=stylesheet TYPE="text/css" HREF="soccer.css"> </head><body bgcolor="#18D018" text="black"> <h2 align="center">Welcome!</h2> <blockquote><blockquote><blockquote> <p align="center"><font size="+1">(Please write this down)<br> <br>Your email address is $email, and your password is $password<br> Please enter <a href="home.htm">here</a>.<br></font></p> </blockquote></blockquote></blockquote></body></html> PrintTag }

2006-10-20 Retitled by planetscape, as per Monastery guidelines: one-word (or module-only) titles hinder site navigation

( keep:0 edit:7 reap:0 )

Original title: 'Newbie'

Replies are listed 'Best First'.
Re: Newbie needs help with email CGI script
by kilinrax (Deacon) on Oct 30, 2000 at 20:31 UTC
    I've never used 'cgi-lib.pl', so I'd use CGI.pm instead ;-)
    Try replacing everything from the shebang to the first if statement with:
    #!/usr/bin/perl -w eval 'exec perl -w -S $0 ${1+"$@"}' if 0; # not running under some shell require 5; use strict; use CGI; my $cgi = new CGI; my $email = $cgi->param('visitor_email_address'); my $password = $cgi->param('visitor_password'); print $cgi->header('text/plain');
    Also, if you want to check the validity of the email address people submit, using Email::Valid is much more reliable than using a regexp.
    Hope this helps ;-)
(Ovid) Re: Newbie needs help with email CGI script
by Ovid (Cardinal) on Oct 30, 2000 at 21:35 UTC
    kilinrax is absolutely correct about not using cgi-lib.pl. Many CGI books still recommend this library, but it is out of date and no longer being maintained. In addition to his above comments, I would strongly suggest that you use taint checking. This script does not appear to actually need it, but as you write more and more scripts, you will need it to protect the integrity of your site. See perlsec for more details.

    Your regex for testing for a valid e-mail address does not work. Below is a snippet explaining the use of Email::Valid. Email::Valid cannot catch all valid e-mail addresses as RFC822 allows for arbitrarily nested comments in e-mail addresses, but will probably correctly identify 99% or more of the e-mail addresses submitted.

    #!/usr/bin/perl -wT use strict; use CGI; use Email::Valid; my $cgi = CGI-new(); my $email = $cgi->param( 'visitors_email_address' ); my $password = $cgi->param( 'visitor_password' ); my $valid_address = Email::Valid->address( $e-mail ); if ( $valid_address ) { # Address is valid, do stuff here # This DOES NOT mean that the address is real } else { # Address is invalid. }
    Additionally, I noticed that you are not consistently checking the return calls on your open statements. If one fails to open, your script will silently continue and you won't know why.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.