Jump to content

Loop through array and find match


meltingpoint

Recommended Posts

This is not working.  I need to query a flat file that looks like this;

user1|pass1|

user2|pass2|

 

and see if it matches for a login.  Can some one see what I am

doing wrong?

 

Thanks

 

//Open file and place each line as an array

$openedfile = file($db_file);

 

//Get number of lines in file

$size = sizeof($openedfile);

 

foreach($openedfile as $Key => $Val)

{

$Data[$Key] = explode("|", $Val);

}

for($K =0; $K<$size; $K++)

{

$user = $Data[$K][0];

$psw= $Data[$K][1];

 

if(($user ==$username) && ($psw == $password))

{

echo" We have a match";

}

else

{

echo" SORRY- No match here";

}

}

Link to comment
Share on other sites

...

foreach($openedfile as $Key => $Val)
   {
   $Data = explode("|", $Val);

   $user = $Data[0];
   $psw= $Data[1];
         
   if($user ==$username && $psw == $password)
      {
      echo" We have a match";
      }
      else
      {
      echo" SORRY- No match here";
      }
   }

...

 

Should work. I'd recommend this method though:

 

$openedfile = file($db_file);

if (in_array($username . '|' . $password, $openedfile))
{
    // match
}
else
{
    // no match
}

 

Why not just use a database?

Link to comment
Share on other sites

Well- here was my error;

SORRY- No match here SORRY- No match here SORRY- No match here SORRY- No match here

Notice: Undefined offset: 1 in c:\program files\easyphp1-8\www\php_testing\testing\db_login\login_check_user.php on line 54

SORRY- No match here

 

? Use a database.  Because I do not know MySQL

 

? Use 2nd method you mentioned. - Well, I am trying to learn to loop through an array and uses/test its components.  Additionally- What I hope to do is if the username and password match- I would then like to take use that info later in the script by calling $user and $psw. 

 

 

Hope that makes sense.

 

Link to comment
Share on other sites

Well- here was my error;

SORRY- No match here SORRY- No match here SORRY- No match here SORRY- No match here

Notice: Undefined offset: 1 in c:\program files\easyphp1-8\www\php_testing\testing\db_login\login_check_user.php on line 54

SORRY- No match here

 

? Use a database.  Because I do not know MySQL

 

? Use 2nd method you mentioned. - Well, I am trying to learn to loop through an array and uses/test its components.  Additionally- What I hope to do is if the username and password match- I would then like to take use that info later in the script by calling $user and $psw. 

 

 

Hope that makes sense.

 

 

How do you define "knowing" MySQL?  You could just do a quick google search and find sample queries that would suit your purposes just fine.  Most of it is PHP...you only need like one line of SQL knowledge.

 

In regards to taking info later in the script...use sessions.

Link to comment
Share on other sites

In regards to taking info later in the script...use sessions.

 

I'd disagree there. Sessions should really be used for session data, not data for a single script.

 

Additionally- What I hope to do is if the username and password match- I would then like to take use that info later in the script by calling $user and $psw. 

 

try this:

 

foreach($openedfile as $Key => $Val)
{
   $Data = explode("|", $Val);
   $user = $Data[0];
   $psw= $Data[1];
         
   if($user ==$username && $psw == $password)
   {
      echo" We have a match";
   }
   else
   {
      echo" SORRY- No match here";
   }
}

 

I'm not sure where you originally get $username and $password from, but you should be able to use them later in the script if there's a match.

Link to comment
Share on other sites

$username and $password were passed to the script from a form.  So set them like so;

 

$username = $_POST['username'];

$password = $_POST['password'];

 

Any suggestions as to why the error mentioned in the my previous post occured?

Problem with code or easyphp?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.