Link to home
Start Free TrialLog in
Avatar of 4099aol
4099aol

asked on

Is this string in the data file?

What I am trying to do is see if a string of text ($usrname) is located in Users.txt.  If it is then I want the script to continue, if not then I want the sub function Login to be called.  I tried the codes below, but it doesn't do anything and just continues even if the string is not in the Users.txt.

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

      open (DAT,"$DataDir/Users.txt");
      if ($LOCK_EX)
         {
            flock(DAT, $LOCK_EX); #Locks the file
       }
      @database_array = <DAT>;
      close(DAT);

      foreach $lines(@database_array)
         {
            chomp($lines);            
            unless($lines =~ /$usrname/i)
               {
                  &Login;
               }
         }
Avatar of ozo
ozo
Flag of United States of America image

Do you want &Login to be called on every line?
What is the expression in $username ?
Avatar of 4099aol
4099aol

ASKER

No I only want to run &Login if it was not found at all (only run it once).  $Usrname is a two variables with a | between each one (email_address|password).
If either variable is empty, the expression will match on anything.
Instead of the foreach loop, just try..

  &Login unless (@database_array =~ /$usrname/i);

does @database_array = <DAT> contain the whole file?  I didn't know you could do this..
 
Avatar of 4099aol

ASKER

So I should have this?

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

      open (DAT,"$DataDir/Users.txt");
      if ($LOCK_EX)
         {
            flock(DAT, $LOCK_EX); #Locks the file
}
      @database_array = <DAT>;
      close(DAT);

      &Login unless (@database_array =~ /$usrname/i);
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
Avatar of 4099aol

ASKER

I am not sure, I found the line in another script and I thought it would do the job I needed.
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
Disregard last commect.. Guess I should not have reposted that form data!
Avatar of 4099aol

ASKER

Ok, well it is working (I think).  Please give me a few minutes to check.
Avatar of 4099aol

ASKER

Nope, I tried to use the script and even if my email address and password (that is what is located in the string) in in the data file (Users.txt) it doesn't work.
Avatar of 4099aol

ASKER

Opps.  What I ment to say is even if the correct information (email|password) is located in the Users.txt file it still doesn't let me continue it just keeps going to &Login;

thanks
Yes, @database_array = <DAT> will read in the entire file.
(Assuming that the open succeded, which I don't see being checked.)
But @database_array in scalar context is the number of elements in the array, so
(@database_array =~ /$usrname/i)
will unly match if $usrname mtches the digits in the number of lines in the file
You might have been thinking of
("@database_array" =~ /$usrname/i)
although I would still want to verify that /$usrname/ is matching what you intend
Avatar of 4099aol

ASKER

ozo, I am not sure what you want.
Your original program seems to call &Login for every element of @database_array that doesn't match the expression in $usrname
If &Login is never called, then the expression in $usrname would seem to match all elements of @database_array
The easiest ways for this to happen are for @database_array to be empty, or for $usrname to be an expression which matches everything.
So far, I haven't seen enough evidence to rule out either of those possibilities.
Avatar of 4099aol

ASKER

Well &Login is executed everytime and I know that the string is in the file (I checked).

thanks
Is the string in every line of the file?
If not, &Login would be executed for each line which does not have it.
Avatar of 4099aol

ASKER

No it will only be in the file once.  The data file (Users.txt) keeps track of the users (members) of my service.  So they will only be in it once.
If the expression only matches once, and the file has more than one line,
then there must be a line which doesn't match.
That line will call &Login
Avatar of 4099aol

ASKER

Ok, now I do not think that we are seeing eye-to-eye.  The string will have the username (in this case the user's email) followed by a | (to seperate) and then the user's password.  What I did was (refer to codes below) added the three together in the string $usrname.

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

I used the function $form_data which is from the cgi-lib.pl library file.  

Now, what I am trying to do with the other codes I posted is see if $usrname is located in the data file (Users.txt) at least once!  If it is in there then just continue, but if it isn't I want the user to have to relogin.  When I call the &Login function it requires that the user enter their username and password agin.

thanks
I would usually do the following:

$gotit = 0;
LINE: foreach $lines(@database_array) {
   chomp($lines);            
   if ($lines =~ /$usrname/i) {
      $gotit = 1;
      last LINE;
   }
}
if ($gotit == 0) { &Login; }

ASKER CERTIFIED SOLUTION
Avatar of rajgn
rajgn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 4099aol

ASKER

Where do I put it?