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;
}
}
$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;
}
}
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..
&Login unless (@database_array =~ /$usrname/i);
does @database_array = <DAT> contain the whole file? I didn't know you could do this..
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);
$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?
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!
ASKER
Ok, well it is working (I think). Please give me a few minutes to check.
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.
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
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
(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
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.
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.
ASKER
Well &Login is executed everytime and I know that the string is in the file (I checked).
thanks
thanks
Is the string in every line of the file?
If not, &Login would be executed for each line which does not have it.
If not, &Login would be executed for each line which does not have it.
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
then there must be a line which doesn't match.
That line will call &Login
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
$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; }
$gotit = 0;
LINE: foreach $lines(@database_array) {
chomp($lines);
if ($lines =~ /$usrname/i) {
$gotit = 1;
last LINE;
}
}
if ($gotit == 0) { &Login; }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Where do I put it?
What is the expression in $username ?