develooper Front page | perl.beginners | Postings from January 2002

Re: Array Problem

Thread Previous | Thread Next
From:
maureen
Date:
January 21, 2002 08:40
Subject:
Re: Array Problem
Message ID:
3C4C4443.DE42F52@home.com
Thanks so much for your help on this. I tried this suggestion, but
unfortunately, the array @indata does not seem to contain the usernames
from the file pwdata.txt.

I've been looking at this for hours. I hope someone can help me figure
out what I am missing here. The objectives for this code and the revised
code follow:

Code Objectives:
1)Accept username and password from an HTML page
2)Open a text file and store the username and passwords listed there in
an array
3)Compare the username and password in the array to the username and
password entered on the HTML page.
4)If username and password match, direct the user to another web page.
5) If username and password do not match or fields are left blank on the
HTML form, direct user to an error page.

Revised code:
#!/usr/local/bin/perl  
require "cgi-lib.pl"; 
#process incoming form data  
&ReadParse; 
#open the database in read-only mode  
open(FILE,"pwdata.txt") || die "Can't find database\n"; 
#store database contents in an array and close file  
@indata = <FILE>;  
close(FILE); 
foreach $i (@indata)   
{ 
#remove hard return character from each record  
chomp($i); 
#split fields on pipe character   
#assign a variable name to each of the fields  
($username,$password) = split(/\|/,$i);
if ($username ne /$in{username}/) 
{ 
#invalid password--create error message and exit  
print &PrintHeader; 
print <<"PrintTag";  
<HTML>  
<HEAD>  
<TITLE>Error!</TITLE>  
</HEAD>  
<BODY BGCOLOR="white" TEXT="black">  
<H1>Authorization Required</H1>  
<BLOCKQUOTE>  
You do not have authorization to enter this website. Please click <a
href="http://www.worldwidewebstrategies.com">here</a>
to return to the WWWS web site.
</BLOCKQUOTE> 
<BLOCKQUOTE>  
If you feel you have received this message in error, please return to
the
login screen and try to enter your username and password again.  
 </BLOCKQUOTE>  
</BODY>  
</HTML>  
PrintTag
exit(0);  
} 
#check for blank form fields  
if ($in{'username'}eq"" || $in{'password'}eq"")  
{ #invalid password--create error message and exit  
print &PrintHeader; 
print <<"PrintTag";  
<HTML>  
<HEAD>  
<TITLE>Error!</TITLE>  
</HEAD>  
<BODY BGCOLOR="white" TEXT="black">  
<H1>Authorization Required</H1>  
<BLOCKQUOTE>  
You do not have authorization to enter this website. Please click <a
href="http://www.worldwidewebstrategies.com">here</a>
to return to the WWWS web site.
</BLOCKQUOTE> 
<BLOCKQUOTE>  
If you feel you have received this message in error, please return to
the
login screen and try to enter your username and password again.  
 </BLOCKQUOTE>  
</BODY>  
</HTML>  
PrintTag
exit(0);  
}; 
#check for existence of lock file  
if (-e "lock.fil")   
{ 
#lock file exists! print message & shut down   
print &PrintHeader;
print <<"PrintTag";
<HTML>  
<HEAD>  
<TITLE>File in use</TITLE>  
</HEAD>  
<BODY BGCOLOR="white" TEXT="black">  
<H1>Try again!</H1>  
<BLOCKQUOTE>  
The database is in use. Please try again later.  
</BLOCKQUOTE>  
</BODY>  
</HTML>  
PrintTag
exit(0);
}
#everything is okay. Create lock file.  
open(LOCK_FILE, ">lock.fil"); 
#open, append record, and close database  
open(FILE,">>data.txt") || die "Can't find database\n";  
print FILE
"$in{'username'}|$in{'password'}\n";
close(FILE); 
#close lock file  
close(LOCK_FILE); 
#delete lock file  
unlink("lock.fil");
#print database contents  
print "Location:http://www.worldwidewebstrategies.com\n\n";
};

Leon wrote:
> 
> ----- Original Message -----
> From: "maureen" <conlon434@home.com>
> To: <beginners@perl.org>
> 
> > Currently, the array seems to only be picking up the last name listed in
> > the text file.
> 
> > @indata = <FILE>;
> > close(FILE);
> > foreach $i (@indata)
> > {
> > #remove hard return character from each record
> > chomp($i);
> > #split fields on pipe character
> > #assign a variable name to each of the fields
> > ($username,$password) = split(/\|/,$i);
> > }
> <snip off>
> > #check for proper password
> > if ($username!=~/$in{'username'}/)
> > {
> > #invalid password--create error message and exit
> > print &PrintHeader;
> 
> In the foreach loop, after iteration, $username,$password received the last
> line of the file. What you really want is to check
> $in{'username'} against every line of the file, to do this, you must check
> within the foreach loop like this :-
> 
> foreach $i (@indata){
>              chomp $i;
>              ($username,$password) = split(/\|/,$i);
>              if ($username !~ /$in{username}/) {
>                 # I would prefer to use ne instead of !~
>                 #invalid password--create error message and exit
>                 print &PrintHeader;
>                 .......
>              };
> };
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org

-- 
Be the change you want to see in the World    - Mahatma Ghandi

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About