PHP / mySQL login failed
I put together a php user script username and while I was able to get the registration page to work (thus excluding the contents of my common.php file as a problem) and verified in mySQL that the database is filling up I can't seem to get the login to post that -or anything other than unsuccessful.
I am definitely typing in the username and password found in the database. Can anyone see where I am going wrong, or advise how I would check what is wrong?
The jmp_users table has the structure:
jmp_userID / init(11) / auto_increment
jmp_username / varchar(30) / utf8_unicode_ci
jmp_password / varchar(40) / utf8_unicode_ci
salt / char(16) / utf8_unicode_ci
and my login.php page:
<?php
require("common.php");
$submitted_username = '';
if(!empty($_POST))
{
$query = "
SELECT
jmp_userID,
jmp_username,
jmp_password,
salt
FROM jmp_users
WHERE
jmp_username = :username
";
$query_params = array(
':username' => $_POST['jmp_username']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$login_ok = false;
$row = $stmt->fetch();
if($row)
{
$check_password = hash('sha256', $_POST['jmp_password'] . $row['salt']);
for($round = 0; $round < 65536; $round++)
{
$check_password = hash('sha256', $check_password . $row['salt']);
}
if($check_password === $row['jmp_password'])
{
$login_ok = true;
}
}
if($login_ok)
{
unset($row['salt']);
unset($row['jmp_password']);
$_SESSION['user'] = $row;
header("Location: private.php");
die("Redirecting to: private.php");
}
else
{
print("Login Failed.");
$submitted_username = htmlentities($_POST['jmp_username'], ENT_QUOTES, 'UTF-8');
}
}
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" value="<?php echo $submitted_username; ?>" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
source to share
since you are using SHA256 (with hex values, not raw) you need 64 characters to store the password hash (you have 40 in total).
btw: I think re-hashing the password 65536 times is unnecessary and the CPU is wasted. In addition, usually a single salt string is used for all passwords.
source to share