FAQ
Hello,
Here is code fragment that I can't seem to solve.
All help, suggestions are appreciated.
Thanks!
Dave

What I want to do is to check for either if the username or password is good:

The $pwfile contains (exactly like it is here):
username1|password1
username2|password2
ect.....


# stuff $pwd in to @indata
open(FILE,$pwfile) || die &dead("Can't find $pwfile. $!");
@indata = <FILE>;
close(FILE);


# populate a hash , I am guessing here!
foreach (@indata){
($x,$y) = split(/\|/,$_);
$users{$x} = $y;
}


# loop and process each record from $pwd
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);


## want to check here and if something is not matching call our sub &dead
while (($test_username,$test_password) = each %users) {
# print "$test_username,$test_password\n";
# print "$in{'name'} $in{'pwd'} \n";
#&dead("Bad username , Sorry") if ($in{'name'} ne $test_username);
#&dead("Password, Sorry") if ($in{'pwd'} ne $test_password);
}


---more deleted----

*====================================================*
* Cora Connection Your West African Music Source *
* http://www.coraconnection.com/ *
* Resources, Recordings, Instruments & More! *
*====================================================*

Search Discussions

  • M.W. Koskamp at Apr 21, 2001 at 1:02 am
    ----- Original Message -----
    From: David Gilden <dowda@coraconnection.com>
    To: <beginners@perl.org>
    Sent: Saturday, April 21, 2001 2:49 AM
    Subject: Populating Hashs and checking multi form fields


    # stuff $pwd in to @indata
    open(FILE,$pwfile) || die &dead("Can't find $pwfile. $!");
    @indata = <FILE>;
    This will only read one line from the file, Is that what you want? If no
    use: @indata = FILE->getlines();
    close(FILE);

    # populate a hash , I am guessing here!
    foreach (@indata){
    ($x,$y) = split(/\|/,$_);
    $users{$x} = $y;
    }
    You might do the chomp in the loop above
    ($x,$y) = split(/\|/,chomp($_));

    # loop and process each record from $pwd
    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);
    whats code above this doing? Yuo did already split it.
    ## want to check here and if something is not matching call our sub &dead
    while (($test_username,$test_password) = each %users) {
    # print "$test_username,$test_password\n";
    # print "$in{'name'} $in{'pwd'} \n";
    #&dead("Bad username , Sorry") if ($in{'name'} ne $test_username);
    #&dead("Password, Sorry") if ($in{'pwd'} ne $test_password);
    }
    You dont want to give people information on what is wrong when they are
    guessing username or password.


    Try something like this


    &dead("Password, Sorry") if !authorize($user, $pwd);

    Sub autorhorize{

    my ($user, $pwd) =@_;
    open (FH, "<$pwfile") || die $!;
    while (<FH>) {
    return 1 if $_ eq "$user|$pwd";
    }
    }
  • King, Jason at Apr 22, 2001 at 11:59 pm

    -----Original Message-----
    From: M.W. Koskamp
    Sent: Sat 21 Apr 2001 11:08
    To: David Gilden; beginners@perl.org
    Subject: Re: Populating Hashs and checking multi form fields



    ----- Original Message -----
    From: David Gilden <dowda@coraconnection.com>
    To: <beginners@perl.org>
    Sent: Saturday, April 21, 2001 2:49 AM
    Subject: Populating Hashs and checking multi form fields


    # stuff $pwd in to @indata
    open(FILE,$pwfile) || die &dead("Can't find $pwfile. $!");
    @indata = <FILE>;
    This will only read one line from the file, Is that what you
    want? If no use: @indata = FILE->getlines();

    David .. ignore the above .. your code is correct

    M.W. .. the diamond operator in list context returns all lines in the file
    .. each line as a separate element in the list .. so after David's code
    above each line from the file is an element in the @indata array
    Try something like this


    &dead("Password, Sorry") if !authorize($user, $pwd);

    Sub autorhorize{

    my ($user, $pwd) =@_;
    open (FH, "<$pwfile") || die $!;
    while (<FH>) {
    return 1 if $_ eq "$user|$pwd";
    }
    }
    this will never return '1' because $_ will always contain an line-ending
    character .. so the equality test will always fail

    --
    jason king

    In Spearfish, South Dakota, if three or more Indians are walking down
    the street together, they can be considered a war party and fired
    upon. - http://dumblaws.com/
  • King, Jason at Apr 23, 2001 at 12:11 am
    David Gilden writes ..
    Here is code fragment that I can't seem to solve.
    All help, suggestions are appreciated.
    Thanks!
    Dave

    What I want to do is to check for either if the username or
    password is good:

    The $pwfile contains (exactly like it is here):
    username1|password1
    username2|password2
    ect.....


    # stuff $pwd in to @indata
    open(FILE,$pwfile) || die &dead("Can't find $pwfile. $!");
    @indata = <FILE>;
    close(FILE); all fine
    # populate a hash , I am guessing here!
    foreach (@indata){
    ($x,$y) = split(/\|/,$_);
    $users{$x} = $y;
    }
    oops .. $y still contains the end-of-line character here .. you probably
    didn't want that .. so instead you should use

    chomp( $users{$x} = $y);

    which chomps off that character as the assignment is done
    # loop and process each record from $pwd
    foreach $i (@indata) {
    so here you get a bit confused .. you've already gone through @indata and
    stored the usernames and passwords in the %users hash .. so there's no need
    to loop again .. you just want the following lines
    while (($test_username,$test_password) = each %users) {
    # print "$test_username,$test_password\n";
    # print "$in{'name'} $in{'pwd'} \n";
    #&dead("Bad username , Sorry") if ($in{'name'} ne $test_username);
    #&dead("Password, Sorry") if ($in{'pwd'} ne $test_password);
    }
    now the equality test has a chance of succeeding because in the %users hash
    you're not storing the line-ending character .. so before you were basically
    trying to compare

    "username1" with $in{name}

    which would have been ok .. but then you also compared

    "password1\n" with $in{pwd}

    which would almost certainly never match

    --
    jason king

    In Spearfish, South Dakota, if three or more Indians are walking down
    the street together, they can be considered a war party and fired
    upon. - http://dumblaws.com/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 21, '01 at 12:50a
activeApr 23, '01 at 12:11a
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase