MDanz Posted July 31, 2009 Share Posted July 31, 2009 $numrows = mysql_num_rows($query); if ($numrows!=0) { while ($row = mysql_fetch_assoc($query)); { $dbusername = $row ['username']; $dbpassword = $row ['password']; } //check to see if they match if ($username==$dbusername&&$password==$dbpassword) { echo "Welcome!"; } else echo "Incorrect Password!"; It keeps saying incorrect password.. only time it said welcome is when i deleted the echo "incorrect password" ; Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/ Share on other sites More sharing options...
sKunKbad Posted July 31, 2009 Share Posted July 31, 2009 if ($numrows!=0) should have an double equals == I think I was cross-eyed... I didn't even see the exclamation point.. Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887279 Share on other sites More sharing options...
MDanz Posted July 31, 2009 Author Share Posted July 31, 2009 tried it doesnt work... :'( Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887283 Share on other sites More sharing options...
sKunKbad Posted July 31, 2009 Share Posted July 31, 2009 Where are the values for $username and $password coming from? Also, what is your query? Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887284 Share on other sites More sharing options...
Philip Posted July 31, 2009 Share Posted July 31, 2009 Is the password in the DB hashed (e.g.. did you use md5() when storing it?) Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887285 Share on other sites More sharing options...
MDanz Posted July 31, 2009 Author Share Posted July 31, 2009 <?php $username = $_POST['username']; $password = $_POST['password']; if($username&$password) { $connect = mysql_connect ("localhost","ustackc1_Master","password")or die("Couldn't Connect"); mysql_select_db("ustackc1_Login")or die("Couldn't Connect"); $query = mysql_query("SELECT * FROM Users WHERE username ='$username'"); $numrows = mysql_num_rows($query); if ($numrows!=0) { while ($row = mysql_fetch_assoc($query)); { $dbusername = $row ['username']; $dbpassword = $row ['password']; } //check to see if they match if ($username==$dbusername&&$password==$dbpassword) { echo "Welcome!"; } else echo "Incorrect Password!"; } else die("That user doesn't exist!"); } else { die ("Please enter a username and password"); } ?> heres the full php Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887286 Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 the problem is you are going through the while loop for all of the possible values of username and password, AND THEN you are comparing them. <?php $numrows = mysql_num_rows($query); if ($numrows!=0) { while ($row = mysql_fetch_assoc($query)); { $dbusername = $row ['username']; $dbpassword = $row ['password']; //check to see if they match if ($username==$dbusername && $password==$dbpassword) { echo "Welcome!"; } else echo "Incorrect Password!"; } edit: technically for logging in you should only be returning one row anyway, so yours will work, but its still bad practice. as a previous poster said, how are you storing the passwords ? One would hope that you hash them before setting them in your database, in which case you would have to hash the input from $_POST before trying to compare it if it still doesn't work, then try echoing $username and $password and $dbusername and $dbpassword and visually compare them and also print them out here so we can see where you could be going wrong Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887287 Share on other sites More sharing options...
MDanz Posted July 31, 2009 Author Share Posted July 31, 2009 this is my second day on php.. i'm not good at it yet. I was following a tutorial on youtube and in comment some people have the same problem.. whats the best way to alter it just to check is password and username are correct? Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887295 Share on other sites More sharing options...
lonewolf217 Posted July 31, 2009 Share Posted July 31, 2009 like we said, we need to know how the passwords are stored in the database. can you post the registration page code ? or at least post the input and output that i requested in my previous post Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887297 Share on other sites More sharing options...
TeNDoLLA Posted July 31, 2009 Share Posted July 31, 2009 Also worth a note. You are comparing here with bitwise operator not logical operator. This happens to work probably for you now but might lead into akward situations later. if($username&$password) Should be instead.. if($username && $password) even better would be to check if they are not empty and exists.. if(!empty$(username) && !empty($password)) Quote Link to comment https://forums.phpfreaks.com/topic/168218-solved-username-and-password-check/#findComment-887298 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.