Solved

Is this string in the data file?

Posted on 1998-10-12
23
164 Views
Last Modified: 2010-03-05
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;
               }
         }
0
Comment
Question by:4099aol
  • 11
  • 6
  • 4
  • +2
23 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205288
Do you want &Login to be called on every line?
What is the expression in $username ?
0
 

Author Comment

by:4099aol
ID: 1205289
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).
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205290
If either variable is empty, the expression will match on anything.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:pcrutch
ID: 1205291
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..
 
0
 

Author Comment

by:4099aol
ID: 1205292
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);
0
 

Expert Comment

by:pcrutch
ID: 1205293
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 

Author Comment

by:4099aol
ID: 1205294
I am not sure, I found the line in another script and I thought it would do the job I needed.
0
 

Expert Comment

by:pcrutch
ID: 1205295
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 

Expert Comment

by:pcrutch
ID: 1205296
Disregard last commect.. Guess I should not have reposted that form data!
0
 

Author Comment

by:4099aol
ID: 1205297
Ok, well it is working (I think).  Please give me a few minutes to check.
0
 

Author Comment

by:4099aol
ID: 1205298
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.
0
 

Author Comment

by:4099aol
ID: 1205299
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205300
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
0
 

Author Comment

by:4099aol
ID: 1205301
ozo, I am not sure what you want.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205302
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.
0
 

Author Comment

by:4099aol
ID: 1205303
Well &Login is executed everytime and I know that the string is in the file (I checked).

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205304
Is the string in every line of the file?
If not, &Login would be executed for each line which does not have it.
0
 

Author Comment

by:4099aol
ID: 1205305
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205306
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
0
 

Author Comment

by:4099aol
ID: 1205307
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
0
 
LVL 1

Expert Comment

by:hutter
ID: 1205308
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; }

0
 
LVL 1

Accepted Solution

by:
rajgn earned 200 total points
ID: 1205309
Your code seems to be right. But there's is a catch in it. I suppose you JUST want to check whether the txt file contains $usrname. In that case when you find one, after calling &Login, you've to come out of the 'foreach' as you don't want to check remaining lines. For that purpose you add a line of code

After the &Login; statement, write

last;

I hope it should run.
0
 

Author Comment

by:4099aol
ID: 1205310
Where do I put it?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question