final60 Posted July 25, 2011 Share Posted July 25, 2011 Hello I am using a farely basic login script for my site. But for some reason sometimes when I logout and try to log back in it will not let me login even though the login details are correct. If I then register with new login details it will let me login with those details but no previous ones. I just can't figure this out. Here is this code login.php <form name="login_form" action="login2.php" method="post"> <table> <tr><td><input type="text" name="username" id="login_fields" value="" /></td><td><input type="password" name="password" id="login_fields" value="" /></td><td><input type="submit" name="submit" value="Login" /></td></tr> </table> </form> login2.php <?php $username = $_POST['username']; $password = $_POST['password']; $submit = $_POST['submit']; if($username&&$password) { $query = mysql_query("SELECT * FROM urbex_users"); //Code to login! while($row = mysql_fetch_assoc($query)) { $db_username = $row['username']; $db_password = $row['password']; $db_userid = $row['user_id']; } //Check to see if they match! if($username==$db_username&&$password==$db_password) { $_SESSION['urbex_username']=$db_username; $_SESSION['urbex_user_id']=$db_userid; echo "<p>You have successfully logged in! Welcome back ". $db_username .".</p>"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php\">"; } else echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>"; } else{ echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>"; } ?> I hope some one can help me with this, it totally has me stumpt! Quote Link to comment https://forums.phpfreaks.com/topic/242783-unable-to-login/ Share on other sites More sharing options...
AyKay47 Posted July 25, 2011 Share Posted July 25, 2011 I would recommend encrypting your password before db insertion $username = $_POST['username']; $password = $_POST['password']; $submit = $_POST['submit']; if(!empty($username) && !empty($password)) //make sure that both fields are filled out.. { $query = mysql_query("SELECT * FROM urbex_users WHERE username = '$username' AND password = '$password'"); $num_rows = mysql_num_rows($query); if($num_rows > 0){ //if there is a match in the database $row = mysql_fetch_array($query, MYSQL_ASSOC); $userid = $row['user_id']; $username = $row['username']; $_SESSION['urbex_username']=$username; $_SESSION['urbex_user_id']=$userid; echo "<p>You have successfully logged in! Welcome back $username</p>"; echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php\">"; } else echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/242783-unable-to-login/#findComment-1246977 Share on other sites More sharing options...
final60 Posted July 25, 2011 Author Share Posted July 25, 2011 Thanks. dammit what an idiot! and yup ill do encryption etc as next step! Quote Link to comment https://forums.phpfreaks.com/topic/242783-unable-to-login/#findComment-1246978 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.