From: "Laird Shaw" <[EMAIL PROTECTED]>

Hi Laird,

Your email help me alot on how to handle this kind of problem.
actually, i solved the problem by adding a variable and additional if:

my $see = 'N';

if ($see eq 'N') {
      if ($username =~ /\b$field2\b/) {
        $see = 'Y';
        }
       else    {
        $see = 'N';
        }
}
}
if ($see eq 'Y') {
      print "OK";
}
else
{
     print "Please try again";
}

I tried your code and it works, thanks again.

glynn


> The problem is that it is printing something for each line regardless of
> whether it has already found a match.  What you need to do is save whether
> you have found a match or not in a variable, then selectively print, ie:
> a) set your variable to be false - ie indicating that you haven't found a
> match yet,
> b) don't print anything if a particular line doesn't match - wait until
the
> end of the loop to print that a match wasn't found (by checking that your
> variable is still false),
> c) print that you have found a match when a particular line matches, store
> in your variable that you have found a match, and then *exit the loop*
> because you don't need to check any more.
>
> I am not sure why you are passing in a password as you don't seem to be
> checking it anywhere.  I am assuming that $field3 is NOT the password - in
> fact I couldn't see any possible password in your file.  Are you checking
> the password at all???
>
> Also, I'm not sure you really want to be using the regular expression you
> are using.  If you use one at all, I would have thought you would want it
> the other way around, ie ($field2 =~ /\b$username\b/).  This means if the
> line in the file read:
>
> user: user1*&^ realm: my.domain.com mech:PLAIN
>
> then entering $username='user1' would match this line.
> With your regular expression of ($username =~ /\b$field2\b/) you are
> matching
> $username='user1*&^'
> to the line in the file:
>
> user: user1 realm: my.domain.com mech:PLAIN
>
> I don't know your needs but it seems to me that you would be better off
> without a regexp at all, just using ($username eq $field2).  I have
modified
> the code accordingly and put in the stuff I explained in a, b, c above
(note
> that as we are not checking password, this will only match the first
> $username found in the file):
>
> #!/usr/bin/perl
> use CGI qw(param);
>
> $username = param('username');
> $password = param('password');
>
> $file= "/home/user/public_html/file.txt";
> print "Content-type: text/plain\n\n";
> #we haven't found a match yet
> my $foundmatch=0;
> open (F1,"$file");
> foreach (<F1>) {
>   chomp;
>   ($field1,$field2,$field3,$field4,$field5,$field6) = split /\s/, $_;
>   if ($username eq $field2) {
>     #only print when we find a match
>     print "OK\n";
>     print "$username and $password\n";
>     #store that we have found a match so we don't print "oops try again"
at
> the end
>     $foundmatch=1;
>     #exit the loop as we don't need to check anymore
>     last;
>   }
> }
> close(F1);
> if (not $foundmatch) {
>   #we didn't find a match as $foundmatch is still false
>   print "oops try again\n";
> }
>
> "Glynn S. Condez" <[EMAIL PROTECTED]> wrote:
>
> >Hi it works now but why is it that it prints a lot of results and it
seems
> >that it reads the
> >file line by line and do the output. Sorry i forgot to mention that the
> >file
> >contains duplicate entries
> >of username, it something like:
> >
> >----snip----
> >user: user1 realm: my.domain.com mech:PLAIN
> >user: user2 realm: my.domain.com mech:PLAIN
> >user: user1 realm: my.domain.com mech: DIGEST-MD5
> >user: user3 realm: my.domain.com mech:PLAIN
> >user: user2 realm: my.domain.com mech:DIGEST-MD5
> >---snip---
> >
> >so if the file contents 10 lines of entries it display 10 lines of
output,
> >here's the real output
> >
> >if match is found, browser prints:
> >oppss try again
> >OK, user1 found
> >oppss try again
> >oppss try again
> >OK, user1 found
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >
> >if no match found, browser prints:
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >oppss try again
> >
> >Is it possible not to display all the entry that match and doesn't match,
> >just only print OK, user1 found and opps try again?
> >
> >what should be added or changed on the code?
> >
> >TIA
> >glen
> >
> >
> >
> >
> >
> >
> >
> > > On Wed, 15 Jan 2003 15:54:02 +0800, [EMAIL PROTECTED] (Glynn
> > > S. Condez) wrote:
> > >
> > > >i have a web form that users can input a username and password and
> >check
> >if
> > > >the username is valid by parsing or extracting the contents of a
file,
> >here
> > > >the web form html:
> > >
> > > I'm not sure what you are trying to do with just the username,
> > > don't you want to check the password too?
> > >
> > > >#!/usr/bin/perl
> > > >use CGI qw(param);
> > > >
> > > >$username = param('username');
> > > >$password = param('password');
> > > >
> > > >$file= "/home/user/public_html/file.txt";
> > > >print "Content-type: text/plain\n\n";
> > > >open (F1,"$file");
> > > >foreach (<F1>) {
> > > >chomp;
> > > >($field1,$field2,$field3,$field4,$field5,$field6) = split /\s/, $_;
> > > >
> > > >if ($username =~ /\b$field2\b/) {
> > > if (($username =~ /\b$field2\b/)and ($password =~ /\b$field3\b/)) {
> > >     print "OK\n";
> > >     print "$username and $password\n";
> > >
> > > }else{
> > >         print "oops try again\n";
> > >       }
> > > }
> > > close(F1);
> > >
> > > >
> > > >and this is the content of the file.txt:
> > > >----snip----
> > > user: user1 password my.domain.com mech:PLAIN
> > >
> > > >user: user2 realm: my.domain.com mech:PLAIN
> > > >user: user3 realm: my.domain.com mech:PLAIN
> > > >user: user4 realm: my.domain.com mech:PLAIN
> > > >---snip---
> > > >
> > > >my problem with this script is, it doesnt display if the
> > > >username is valid or not but valid usernames display
> > > >OK, $username and $password.
> > > >
> > > >whats the problem with the else statement? kindly correct my
> > > >script.
> > > >
> > > >TIA
> > > >glen
> > > >--- Glynn ---
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> >
> >
> >--
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to