I have created two files - secured registration and secured login using hashing in PHP and using MySQL. Everything appears to be working. However, when I login, it doesn't appear to be working for some reason. I cannot pinpoint the underlying issue in secured password. What am I missing in the code somewhere?
Registration:
$firstname = trim($_POST["firstname"]);
$lastname = trim($_POST["lastname"]);
$emailaddress = trim($_POST["emailAddress"]);
$username = trim($_POST["username"]);
$password = trim($_POST["password"]);
$confirmpwd = trim($_POST["confirmpwd"]);
if ($password != $confirmpwd)
{
header('Location: CreateNewAccount.php');
}
if (strlen($username) > 30)
{
header('Location: CreateNewAccount.php');
}
$hash = hash('sha256', $password);
function createSalt() {
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3);
}
$salt = createSalt();
$pwd = hash('sha256', $salt . $hash);
$conn = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$query = $conn->prepare('INSERT INTO Member (firstName, lastName, email, username, password, salt) VALUES (?, ?, ?, ?, ?, ?)');
$query->execute(array($firstname, $lastname, $emailaddress, $username, $pwd, $salt));
header('Location: Login.php');
Login:
$username = $_POST['username'];
$password = $_POST['password'];
$conn = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$query = "SELECT password, salt"
. "FROM Member"
. "WHERE username = :username";
$result = $conn->prepare($query);
$result->bindParam(":username", $username);
$result->execute();
$number_of_rows = $result->rowCount();
if($number_of_rows == 0) // User not found. So, redirect to Login.php
{
header('Location: Login.php');
}
$userData = $result->fetch(PDO::FETCH_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password));
if($hash != $userData['password']) // Incorrect password. So, redirect to Login.php
{
header('Location: Login.php');
} else // Redirect to Log Entry page
{
header('Location: LogEntry.php');
}
Thanks for your help.