I'm having trouble writing a login script that works with my registration script.
register.php
$username = $_POST['signupEmail'];
$password = $_POST['signupPassword'];
$gender = $_POST['signupGender'];
$country = $_POST['signupCountry'];
$salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE));
$password = hash('sha512', $password . $salt);
$query = mysqli_query($con, "INSERT INTO sh_users (username, password, salt, gender, country) Values ('" . $username . "' , '" . $password . "' , '" . $salt . "' , '" . $gender . "' , '" . $country . "')") or die(mysqli_error($con));
This works no problem - all values in the database.
login.php
$query = "SELECT *
FROM sh_users
WHERE username = '$username';";
$result = mysqli_query($con, $query);
$userData = mysqli_fetch_array($result, MYSQL_ASSOC);
$salt = $userData['salt'];
$hash = hash('sha512', $password . $salt);
if ($hash != $userData['password'])
{
echo "Incorrect email or password";
}
else
{
echo "success";
}
If I echo echo $password
then it's the same as what I input (and yes, I'm using the correct password). Likewise, $salt
matches what's in the database. However, $hash
gives a rogue result despite the fact that I'm using the same hashing method in both scripts.
Where am I going wrong?
UPDATE
The problem is that $_POST['signupPassword']
is returning null when I var_dump()
it at the top of register.php
.
The strange thing is that dumping $_POST['signupPassword2']
comes out fine, but I'd like to understand the difference between the two.
Here's my form (which I thought I'd posted the first time, apologies).
<form id="signup-form" action="" method="POST">
<input name="signupEmail" type="email" class="form-control" id="signupEmail" placeholder="Email address">
<input name="signupPassword" type="password" class="form-control" id="signupPassword" placeholder="Password">
<input name="signupPassword2" type="password" class="form-control" id="signupPassword2" placeholder="Password">
<select name="signupCountry" id="signupCountry" class="selectpicker">
<option value="0">Country</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option>Canada</option>
</select>
<select name="signupGender" id="signupGender" class="selectpicker">
<option value="0">Gender</option>
<option value="f">Female</option>
<option value="m">Male</option>
</select>
<button id="signup" class="btn btn-success btn-block signup" type="submit">Sign up</button>
</form>