kch011099
asked on
Comparing data
I have a script that reads the user id and password given by the user and compares it to the data in a text file. If it matches, then it goes to their page. If it doesn't match, then it goes to another page. However, if they only put in their user id, then it still goes to there page. Is there a way I can make it match only if the entire line is there?
most likely you made a mistake in your script. Please provide an excerpt from it where you do comparison - somebody will fix it for you...
ASKER
Here is the code I am using.
#!/usr/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
open(LOG,"<../clientaccess /password. txt")||&Er rorMessage ;
@logmessages = <LOG>;
close<LOG>;
$n=1;
foreach $message (@logmessages) {
if ($message =~ $formdata{$key}) {
($id,$pwd) = split(/,/,$formdata{$key}) ;
if ( $message ) {
use CGI qw/:standard/;
print redirect('http://www.site.com/users/'.$id);
}
$n++;
}
else {
print "Content-type: text/html\n\n";
print "Password is incorrect. Please try again.";
$n++;
}
}
#!/usr/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
open(LOG,"<../clientaccess
@logmessages = <LOG>;
close<LOG>;
$n=1;
foreach $message (@logmessages) {
if ($message =~ $formdata{$key}) {
($id,$pwd) = split(/,/,$formdata{$key})
if ( $message ) {
use CGI qw/:standard/;
print redirect('http://www.site.com/users/'.$id);
}
$n++;
}
else {
print "Content-type: text/html\n\n";
print "Password is incorrect. Please try again.";
$n++;
}
}
Your code only checks for the $key :
if ($message =~ $formdata{$key}) {
You are not checking for the $Value, which I assume is the password.
if ($message =~ $formdata{$key}) {
You are not checking for the $Value, which I assume is the password.
if your password.txt file is in format
user1:passwd1
user2:passwd2
and in your form there is field named "login" for login name and "passwd" for password, then IMHO
you have to change if($message.... with
if ($message eq $formdata{'login'}.':'.$fo rmdata{'pa sswd'}."\n "){
if you edit password file on windows computer, you maight need to chgange "\n" to "\r\n". No extra spaces at the end of the line is allowed.
Good Look
user1:passwd1
user2:passwd2
and in your form there is field named "login" for login name and "passwd" for password, then IMHO
you have to change if($message.... with
if ($message eq $formdata{'login'}.':'.$fo
if you edit password file on windows computer, you maight need to chgange "\n" to "\r\n". No extra spaces at the end of the line is allowed.
Good Look
($message =~ $formdata{$key})
Since this is a pattern match I think you might find this more reliable:
($message =~ /$formdata{$key}/)
or use
$message eq $formdata{$key}
Since this is a pattern match I think you might find this more reliable:
($message =~ /$formdata{$key}/)
or use
$message eq $formdata{$key}
ASKER
Each piece of info. entered does not have a separate key. The key is "passwd" and the user id and password entered is the value.
passwd = user1, password
so my text file looks like this
user1, password1
user2, password2
user3, password3
etc.
I need it to compare the whole line.
passwd = user1, password
so my text file looks like this
user1, password1
user2, password2
user3, password3
etc.
I need it to compare the whole line.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.