yddib Posted August 26, 2008 Share Posted August 26, 2008 Hi, The code I have for login works if the user is in the database however it returns a blank screen rather than the else statement if they enter an incorrect name. This is the code I have so far: <?php $user = htmlentities($_POST['user']); $pass = htmlentities($_POST['pass']) ?> <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40"> <head> <title>You are Being redirected</title> <!--<meta http-equiv="Refresh" content="3; URL=next.html">--> </head> <body> <?php mysql_connect("host", "username", "password") or die(mysql_error()); //connects to the blacknight server mysql_select_db("our database"); //selects the database $result = @mysql_query("SELECT f_name from gradinfo where email ='".$_POST["user"]."' and pass = '".$_POST["pass"]."'"); if (!$result) { echo "Login unsuccessful"; } while ($row = mysql_fetch_array($result)) { echo "Thank you for logging in,". $row['f_name']."<br/>Please click here to continue if your browser does not redirect you."; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/ Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 The $result checking doesn't do anything, it will just check if $result is a resource. You should have: <?php $user = mysql_real_escape_string($_POST['user']); $pass = mysql_real_escape_string($_POST['pass']); $results = mysql_query("SELECT f_name FROM gradinfo WHERE email='$user' AND pass='$pass'"); if(mysql_num_rows($results) == 1){ echo 'You logged in. Yeah'; //the other code } else{ echo 'Incorrect. Roar.'; } ?> Side note: You may consider hashing the password with sha1() or md5(). Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/#findComment-625806 Share on other sites More sharing options...
alin19 Posted August 26, 2008 Share Posted August 26, 2008 f (sizeof($result)>0) { echo "Login unsuccessful"; } Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/#findComment-625807 Share on other sites More sharing options...
asmith Posted August 26, 2008 Share Posted August 26, 2008 people mentioned enough. Just the way i do it : if (mysql_num_rows($result) == 0) { echo "wrong username or password"; } else { echo "thanks for logging in"; } Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/#findComment-625808 Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 f (sizeof($result)>0) { echo "Login unsuccessful"; } sizeof() is an alias of count(). What are you counting, a resource? You should try that code first to be convinced that it does nothing. Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/#findComment-625809 Share on other sites More sharing options...
yddib Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks very much for ye're help. We got it working using: <?php mysql_connect("host", "username", "password") or die(mysql_error()); //connects to the blacknight server mysql_select_db("db1"); //selects the database $result = @mysql_query("SELECT f_name from gradinfo where email ='".$_POST["user"]."' and pass = '".$_POST["pass"]."'"); if (mysql_num_rows($result) == 0) { echo "Incorrect username or password."; } else { while ($row = mysql_fetch_array($result)) { echo "Thank you for logging in, ". $row['f_name'].".<br/>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/121377-solved-login-code-problem/#findComment-625845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.