Hi phdr,
All noted and appreciated. However just to note, in the example, the correct hash is returned and used in password_verify with the correct username.
If no rows are found it returns false.
Below is full user add where hash is generated/added:
require_once('checkKey.php');
$u = $_POST['uKey'];
$p = $_POST['pKey'];
$device = $_POST['PhoneID'];
$username = $_POST['UName'];
$pass = $_POST['UPass'];
$email = $_POST['email'];
$country = $_POST['country'];
$regKey = $_POST['rKey'];
$password;
$auth = checkAuth($u, $p);
if ($auth == "false")
{
echo "FORBIDDEN";
exit;
}
$curDir = getcwd();
require_once($curDir. '/config/mysqli.php');
$db = connectMe();
if ($db)
{
$password = password_hash($pass, PASSWORD_DEFAULT);
$stmt = $db->prepare("INSERT INTO TableReactionUsers (deviceID, username, pass, email, regKey, country) VALUES(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $device, $username, $password, $email, $regKey, $country);
$stmt->execute();
$result = $stmt->affected_rows;
if ($result == 1)
{
echo "true";
}
else
{
echo "false";
}
$result->free();
}
else
{
echo "NOT CONNECTED";
}
And below is login check:
$u = $_POST['uKey'];
$p = $_POST['pKey'];
$user = $_POST['theUser'];
$pass = $_POST['thePass'];
$auth = checkAuth($u, $p);
if ($auth == "false")
{
echo "FORBIDDEN";
exit;
}
$curDir = getcwd();
require_once($curDir. '/config/mysqli.php');
$db = connectMe();
if ($db) {
$stmt = $db->prepare("SELECT pass FROM TableReactionUsers WHERE username=?");
$stmt->bind_param("s", $user);
$stmt->execute();
$result = $stmt->get_result();
$rowsFound = $result->num_rows;
while($row = $result->fetch_assoc()) {
$hashedPass = $row['pass'];
}
///NOTE: THIS WILL SHOW CORRECT PASS & HASH echo $pass. ' ' .$hashedPass;
if (password_verify($pass, $hashedPass)) {
echo 'true';
}
else
{
echo 'false';
}
}
else
{
echo "NOT CONNECTED";
}
Table:
CREATE TABLE TableReactionUsers
(
ID
int(11) NOT NULL,
deviceID
varchar(50) COLLATE utf8_unicode_ci NOT NULL,
username
varchar(50) COLLATE utf8_unicode_ci NOT NULL,
pass
varchar(255) COLLATE utf8_unicode_ci NOT NULL,
email
varchar(100) COLLATE utf8_unicode_ci NOT NULL,
regkey
varchar(20) COLLATE utf8_unicode_ci NOT NULL,
country
varchar(25) COLLATE utf8_unicode_ci NOT NULL,
changePassKey
varchar(255) COLLATE utf8_unicode_ci NOT NULL,
Expiry
datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Jamie