meltingpoint Posted June 24, 2009 Share Posted June 24, 2009 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/ Share on other sites More sharing options...
Adam Posted June 24, 2009 Share Posted June 24, 2009 ... 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? Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862620 Share on other sites More sharing options...
meltingpoint Posted June 24, 2009 Author Share Posted June 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862637 Share on other sites More sharing options...
947740 Posted June 24, 2009 Share Posted June 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862640 Share on other sites More sharing options...
Adam Posted June 24, 2009 Share Posted June 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862650 Share on other sites More sharing options...
947740 Posted June 24, 2009 Share Posted June 24, 2009 Ah...I misunderstood. I thought he meant later on in other scripts. Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862654 Share on other sites More sharing options...
Adam Posted June 24, 2009 Share Posted June 24, 2009 Actually come to think of it, I bet you were right anyway... Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862655 Share on other sites More sharing options...
meltingpoint Posted June 24, 2009 Author Share Posted June 24, 2009 $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? Quote Link to comment https://forums.phpfreaks.com/topic/163490-loop-through-array-and-find-match/#findComment-862662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.