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

I am certian that the variables are equal, but when I try to test them to verify whether the entered password and username are valid, the code always executes as false. Any ideas or suggestions?
unless (("$my_user" eq "$page_usr")&&("$my_passw" eq "$page_passw")) { print "Content-type: text/html\n\n"; print "Wrong Username or Password!\n"; print "Entered User: $my_user\n"; print "Allowed User: $page_user\n"; print "Entered Passw: $my_passw\n"; print "Allowed Passw: $page_passw\n"; exit; }
Thanks!

Replies are listed 'Best First'.
(tye)Re: Password Authentication
by tye (Sage) on Mar 14, 2001 at 04:17 UTC
    unless( $my_user eq $page_usr && $my_passw eq $page_passw ) { print "Content-type: text/plain\n\n"; print "Wrong Username or Password!\n"; print "Entered User: ($my_user)\n"; print "Allowed User: ($page_user)\n"; print "Entered Passw: ($my_passw)\n"; print "Allowed Passw: ($page_passw)\n"; exit; }

    This way you can tell if whitespace is your problem.

    People who come to Perl from shell script often write "$var" when $var will do. If you start dealing with things like objects or other types of references, "$var" can bite you.

            - tye (but my friends call me "Tye")
Re: Password Authentication
by dws (Chancellor) on Mar 14, 2001 at 04:22 UTC
    Are you sure there aren't newlines at the ends of the passwords? (As might happen if you pulled the passwords out of a file without chomp'ing lines.)

    "ToP SeKrEt" isn't equal to "ToP SeKrEt\n"

Re: Password Authentication
by archon (Monk) on Mar 14, 2001 at 04:02 UTC
    First, what makes you certain the variables are equal?
    Second, why are you putting quotes around the variables in the comparison? You should do $my_user eq $page_usr to be more efficient.
    Third, you have $page_usr in the condition and $page_user in the block. That might be your error.
Re: Password Authentication
by Hot Pastrami (Monk) on Mar 14, 2001 at 04:00 UTC
    Make sure capitalization is the same on each... If you don't want to compare in a case-sensitive fashion, try this:

    unless ((uc $my_user eq uc $page_usr)&&(uc $my_passw eq uc $page_passw))

    Hot Pastrami
      just a quick question... do we need all these parenthesis ??
      this works just fine
      if (uc $my_user eq uc $page_usr && uc $my_passw eq uc $page_passw) {
      right??
      Chady | http://chady.net/
        In this case, && has lower precedence than eq, so parens aren't required... but I'd always use parens just to make it clear in code that that's the precedence I wanted.

        --
        Me spell chucker work grate. Need grandma chicken.

        The parenthesis make it easier to read, IMHO.