Grant132 Posted March 3, 2007 Share Posted March 3, 2007 Hello... I've got a simple login system and everything works except for one little problem. It has an error system that will display "An error occured, please re-enter your details." if what you put into the login form doesn't match with the FFDB. That will work if you get it wrong, but if you get the details correct then it will still show the error message ontop of the included page..Is there a way to fix this? <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { $u = $data[$k][0]; $p = $data[$k][1]; if($user == $Admin AND $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; echo "welcome admin"; include("news.php"); break; } if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; include("news.php"); break; } else { session_destroy(); include("wrong_pw.php"); break; } }else { if($pass == $p || $pass != $p){ echo "An error occured, please re-enter your details."; } } } ?> Any help would be much appreciated, Grant Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 <?php if($pass == $p || $pass != $p){ echo "An error occured, please re-enter your details."; } ?> you are showing the error even if the password is right, get rid of the $pass == $p || and you should be good Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 nope..still getting the same problem Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 Hmm.. Your code should read as : if($pass != $p){ echo "An error occured, please re-enter your details."; } Just asking cause I'm not seeing where it is messing up. Some thoughts though, echo out the variables to see what you are getting. Also, get rid of that if completely because it is not needed. Inside your if ($user = $u) you check to see if the password matches, you can just echo it out in that if. A thing I am confused at is the for loop, it looks like you are checking multiple users in, i could just be misreading it since I'm not too familiar with that code. Good luck, sorry I'm not much more of any help. The14thGOD Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 what do you mean checking multiple users in? like there is a "db.php' which stores all the usernames and passwords which is what that statements for. And i don't think its the variables that are the problem. The logics to it is that while its scanning thought that db.php looking for the matching set to your input, it will output an error each time it doesnt match. like if the details your entering are the 6th line then you will get 5 error messages etc. So i guess its the for loop causing the problems...but is there another way to do it? =S Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 ya I understand why it's going through the list. try doing something like <?php if($user ==$data[k][0]) { //insert the rest of the code in here after the for loop puts data into variables $u and $p } ?> That may work? /shrug Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 YES!!! OMG YES!! it actually works!! except for one thing.. lol if you enter a incorrect username it won't show anything..so i tried <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { if($user == $data[$k][0]) { $u = $data[$k][0]; $p = $data[$k][1]; if($user == $Admin AND $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; echo "welcome admin"; include("news.php"); break; } else { if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; include("news.php"); break; } } } if($pass != $p) { echo "An error occured, please re-enter your details."; break; } } elseif($user != $data[$k][0]) { echo "wrong username"; } } ?> but then its showing again when you enter the correct details..any ideas? =D Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 hmm, well the reason is because at some point that will be correct. there are 2 ways i can think of, at the end of the if $user = $data[$k][0] put a exit(0); this well stop it, and nothing after it will be executed, or, this is the way i would do it. change it so that if the user is valid send them to a different page with the info that they need on it using the: header("Location page.php"); exit(0); else ( error = "Please re-enter your information"; header("Location login.php?error=$error then the login page can use the $_GET[error], though that sounds like a lot of work hah Up to you, if you need me to explain further let me know. Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 hmm that seems a bit complicated for me just yet...so i might use the exit(0); method. By at the end do you mean before the else? or before the last } of the if statement or what? Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 Before the } that closes "if($user == $data[$k][0]) {" It will only get into that statement if that is true, so it would stop executing the rest of the code after that } Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 hmm is this right? <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { if($user == $data[$k][0]) { $u = $data[$k][0]; $p = $data[$k][1]; if($user == $Admin AND $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; echo "welcome admin"; include("news.php"); break; } else { if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; include("news.php"); break; } } } if($pass != $p) { echo "An error occured, please re-enter your details."; break; } exit(0); } else { echo "wrong username"; } } ?> cause that doesnt want to work Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 put it here: <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { if($user == $data[$k][0]) { $u = $data[$k][0]; $p = $data[$k][1]; if($user == $Admin AND $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; echo "welcome admin"; include("news.php"); break; } else { if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; include("news.php"); break; } //Me here exit(0); } } if($pass != $p) { echo "An error occured, please re-enter your details."; break; } } else { echo "wrong username"; } } ?> Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 oh ok..still getting the "wrong username" coming up when you login tho =( Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 hmm..alright try this <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $bad_user = true; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { if($user == $data[$k][0]) { $u = $data[$k][0]; $p = $data[$k][1]; } } if($user == $Admin and $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; $bad_user = false; echo "welcome admin"; include("news.php"); break; } else { if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; $bad_user = false; include("news.php"); break; } } } if($pass != $p and $bad_user == true) { echo "An error occured, please re-enter your details."; break; } else { echo "wrong username"; } ?> This is how this works, first of all we set the $bad_user to true, if the username matches an entry, then we set the variables u and p, if it matches both username and password we set the value to true, so that the only way to get false is if the username or password dont match up. also, if you dont mind, try to keep your code similar to the way it is formated here (i had to change something so the $bad_user=true is a little off. it's just easier to troubleshoot cuase you can see the end of the if/for etc statements, and is cleaner to read to *****EDIT***** added a second "=" to the last statement since it was setting the value to true =P Quote Link to comment Share on other sites More sharing options...
Grant132 Posted March 3, 2007 Author Share Posted March 3, 2007 ok, had to make a few changes there but it works perfectly now ^^ <?php session_start(); $user = $_POST['username']; $pass = $_POST['password']; $Admin = "Admin"; $test = "reallysecureadminpassword"; $bad_user = true; $details = file("db.php"); foreach($details as $key => $Val) { $data[$key] = explode("||", $Val); } for($k = 0; $k < sizeof($details); $k++) { if($user == $data[$k][0]) { $u = $data[$k][0]; $p = $data[$k][1]; } } if($user == $Admin and $pass == $test) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; $bad_user = false; echo "welcome admin"; include("news.php"); } else { if($user == $u) { if($pass == $p) { $_SESSION['user'] = $user; $_SESSION['logged'] = "yes"; $_SESSION['test'] = "1"; $bad_user = false; include("news.php"); } } } if($pass != $p || $pass != $test and $bad_user == true) { echo "An error occured, please re-enter your details."; } ?> Had to remove all the break; 's as i was getting fatal errors from them lol. And i had to add || $pass != $test to that last if statement so that i wouldn't get the error message when i logged in as the admin. Just going to test it a bit more but that works so far. Thankyou soo much for the help The14thGOD =) much appreciated oh and sorry about the formatting. Quote Link to comment Share on other sites More sharing options...
The14thGOD Posted March 3, 2007 Share Posted March 3, 2007 you're very welcome, good luck The14thGOD Quote Link to comment 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.