[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

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?
0
kch011099
Asked:
kch011099
  • 2
  • 2
  • 2
  • +1
1 Solution
 
monasCommented:
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...
0
 
kch011099Author Commented:
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")||&ErrorMessage;
@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++;
      }
}
0
 
KennyIT Application ExecutiveCommented:
Your code only checks for the $key :

if ($message =~ $formdata{$key}) {

You are not checking for the $Value, which I assume is the password.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
monasCommented:
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'}.':'.$formdata{'passwd'}."\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
0
 
olthoffCommented:
($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}
0
 
kch011099Author Commented:
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.
0
 
olthoffCommented:
Then it sounds like your redirection should be like this:

open(PWD, passwordfile) || die;
while (<PWD>) {
  chomp;
  if (m/^$user/) { # The user exists
    if ("$user, $password" eq $_) {
      #The password is correct
    };
  };
};
close(PWD);
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now