I am trying to create a login form, but the username and password validation isn't working properly. $AccountValidation
is always returning 0
, while I am positive I filled in the right password and username. First a user has to register with more variables (i.e. first name) then they can go to the login.html
page and then they go to the file below login.php
.
I have var_dumped
all variables and all are coming through nicely.
$Username = strip_tags($_POST['Username']);
$Password = md5(strip_tags($_POST['Password']));
if(empty($Username) || empty($Password)) die('Vul alle waarden in op <a href="login.php">het formulier</a>.');
$db = new mysqli($host, $user, $pass, 'accounts', 3306);
$sql = "SELECT Username, Password FROM users where Username = '$Username' AND Password = '" . $Password . '\'';
$results = $db->query($sql);
$AccountValidation = $results->num_rows;
if($AccountValidation == 1)
{
$_SESSION['login'] = 'yes';
header('location: xxxxx');
} else{
echo'Your account does not exist or you have not filled in the required information.<br> Retry <a href="Login.html">here</a> or create a new account <a href="GetInfo.html">here</a>';
}
?>
var_dump $results
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
var_dump $AccountValidation
int(0)
Edit:
Found the solution. MD5 has 32 characters and I allowed only 20 for a password to be uploaded to MySQL. Sometimes it can be this stupid...