bloodgoat Posted March 21, 2009 Share Posted March 21, 2009 Just working with the bare bones of this right now. Mostly a learning experience, it doesn't have any real application yet, but nonetheless I still want to nail it. I'm refusing to work with SQL, otherwise I don't think I'd have this problem (as every online guide is based on an SQL/PHP combination). Here are my users.php and check_login.php file codes: USERS.PHP <?php $user_list = array("user1", "user2", "user3"); $pass_list = array("pass1", "pass2", "pass3"); ?> CHECK_LOGIN.PHP <?php include("users.php"); foreach($pass_list as $pass){ foreach($user_list as $user){ if($_POST['username'] == $user && $_POST['password'] == $pass){ echo "Successfully logged in!<br>\n"; echo "You have been logged in as <b>".$_POST['username']."</b>.\n"; } elseif($_POST['username'] != $user || $_POST['password'] != $pass){ echo "You have entered invalid credentials; please try again.<br>\n"; } } } ?> Basically, when it logs it (which works just fine) it displays this: You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. You have entered invalid credentials; please try again. Successfully logged in! You have been logged in as user3. I've tried putting "return;" proceeding everything else in each if() statement, but when I do and I try to log in as anything other than the first user in the $user_list array, it just returns my "invalid credentials" message. I'm fresh out of ideas. Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/ Share on other sites More sharing options...
WolfRage Posted March 21, 2009 Share Posted March 21, 2009 <?php include("users.php"); $valid_user=FALSE; $valid_pass=FALSE; foreach($pass_list as $pass){ if($_POST['password'] == $pass) { $valid_pass=TRUE; } //No need for a else, if it is not in the list then the result is set to FALSE foreach($user_list as $user){ if($_POST['username'] == $user){ $valid_user=TRUE; } } if($valid_user===TRUE && $valid_pass===TRUE){ echo 'Login Successful!'; } else{ echo 'Login Failed!'; } ?> Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790335 Share on other sites More sharing options...
bloodgoat Posted March 21, 2009 Author Share Posted March 21, 2009 So basically, putting them into their own respective loops? Makes sense. Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790344 Share on other sites More sharing options...
RichardRotterdam Posted March 21, 2009 Share Posted March 21, 2009 Drat beat me too it I'm refusing to work with SQL, Uhm why??? thats a usefull skill to have. @WolfRage <?php include("users.php"); $valid_user=FALSE; $valid_pass=FALSE; foreach($pass_list as $pass){ if($_POST['password'] == $pass) { $valid_pass=TRUE; } //No need for a else, if it is not in the list then the result is set to FALSE foreach($user_list as $user){ if($_POST['username'] == $user){ $valid_user=TRUE; } } if($valid_user===TRUE && $valid_pass===TRUE){ echo 'Login Successful!'; } else{ echo 'Login Failed!'; } ?> that won't work for example if you have the following php arrays <?php $valid_user[0]='user0'; $valid_pass[0]='pw0'; $valid_user[1]='user1'; $valid_pass[1]='pw1'; ?> having : user0 + password1 will validate I say use 1 array for users like so <?php $users=array(); $users[]=array('name'=>'user1','password'=>'pw1'); $users[]=array('name'=>'user2','password'=>'pw2'); ?> you now only need one loop <?php $login=false; foreach($users as $users ){ /*echo "name: {$users['name']}<br />"; echo "name: {$users['password']}<br />";*/ if($_POST['username'] == $user['name'] && $_POST['password'] == $user['password']){ $login=true; break;//stop the loop already match found } } ?> Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790345 Share on other sites More sharing options...
bloodgoat Posted March 21, 2009 Author Share Posted March 21, 2009 Drat beat me too it I'm refusing to work with SQL, Uhm why??? thats a usefull skill to have. I said it's a learning experience. I've never used foreach() before, nor have I made a login script before. Two birds/one stone. Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790351 Share on other sites More sharing options...
bloodgoat Posted March 21, 2009 Author Share Posted March 21, 2009 I say use 1 array for users like so <?php $users=array(); $users[]=array('name'=>'user1','password'=>'pw1'); $users[]=array('name'=>'user2','password'=>'pw2'); ?> you now only need one loop <?php $login=false; foreach($users as $users ){ /*echo "name: {$users['name']}<br />"; echo "name: {$users['password']}<br />";*/ if($_POST['username'] == $user['name'] && $_POST['password'] == $user['password']){ $login=true; break;//stop the loop already match found } } ?> I just tried this, exact same thing happens. Even with break, it keeps looping. The problem is it continuously returns the error message until it finds a match. If you log in as the first user in the array, there will be no error messages. But if you log in as the third user in the array, there will be two error messages, one for the first in the array, and another for the second in the array. Pretty much the same problem I had to begin with, just with different code. Regardless, I've been converting my $users into a multi-dimensional array anyways (I want to enable user-specific permissions). So if any gurus want to help out, here's my updated code: USERS.PHP <?php $users = array( /* User Key User Name Password Permissions */ "[1]" => array( "user1", "pass1", "3"), "[2]" => array( "user2", "pass2", "1"), "[3]" => array( "user3", "pass3", "1") ); ?> CHECK_LOGIN.PHP <?php include("users.php"); foreach($users as $key => $user){ if($_POST['username'] == $user[0] && $_POST['password'] == $user[1]){ echo "Successfully logged in!<br>\n"; echo "You have been logged in as <b>".$_POST['username']."</b>.<br>\n"; echo "Your permissions are ".$permissions.".<br>\n"; break; } else { echo "You have entered invalid credentials; please try again.<br>\n"; } } ?> It still returns an invalid login error until it finishes looping (ie/ finds a match). Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790364 Share on other sites More sharing options...
RichardRotterdam Posted March 21, 2009 Share Posted March 21, 2009 oh my bad this: foreach($users as $users ){ should be: foreach($users as $user ){ also your users.php arrays wont work its should be like <?php $users=array(); $users[]=array('name'=>'user1','password'=>'pw1','permissions'=>1); $users[]=array('name'=>'user2','password'=>'pw2','permissions'=>2); ?> Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790369 Share on other sites More sharing options...
bloodgoat Posted March 21, 2009 Author Share Posted March 21, 2009 oh my bad this: foreach($users as $users ){ should be: foreach($users as $user ){ also your users.php arrays wont work its should be like <?php $users=array(); $users[]=array('name'=>'user1','password'=>'pw1','permissions'=>1); $users[]=array('name'=>'user2','password'=>'pw1','permissions'=>2); ?> About the $users to $user, ya, I already considered that when I was trying it and made the necessary adjustments. Everything functioned fine when I fixed the small errors. And about my users.php not working... actually it does. I'm working with it in real-time as I watch this thread. Otherwise, I managed to work out my permissions display problem (woot), I just want this loop of error messages to finally keel over and die. Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790372 Share on other sites More sharing options...
WolfRage Posted March 21, 2009 Share Posted March 21, 2009 Looks like you weren't so hot on the fly either DJKat, so yeah I just put the idea out there, I did not test it for all possibilities. But I appreciate you pointing out the flaw, but don't be so quick. Link to comment https://forums.phpfreaks.com/topic/150477-solved-login-script-userspass-in-array-and-foreach-calling/#findComment-790373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.