$password = $link->real_escape_string($_POST['password']);
$email = $link->real_escape_string($_POST['email']);
$sql = "SELECT password, userID FROM `users` WHERE email = '$email' LIMIT 1";
$result = $link->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$db_password = $row["password"];
if(password_verify($password, $db_password)) {
echo "correct";
} else {
echo "no user found";
}
}
$password = trim(password_hash($password, PASSWORD_BCRYPT, [12]));
<?php // demo/temp_blacksulfur.php
/**
* https://www.experts-exchange.com/questions/28966548/password-verify-issues.html
*
* http://php.net/manual/en/function.password-hash.php
* http://php.net/manual/en/function.password-verify.php
*/
error_reporting(E_ALL);
echo '<pre>';
$pw = 'gooseball';
$h1 = trim(password_hash($pw, PASSWORD_BCRYPT, [12]));
$h2 = password_hash($pw, PASSWORD_DEFAULT);
var_dump($pw, $h1, $h2);
// TEST
if (password_verify($pw, $h1)) echo PHP_EOL . "$pw == $h1";
if (password_verify($pw, $h2)) echo PHP_EOL . "$pw == $h2";
$secret = password_verify($password, $db_password);
echo $secret;
$password = $link->real_escape_string($_POST['password']);
$email = $link->real_escape_string($_POST['email']);
$sql = "SELECT password, userID FROM `users` WHERE email = '$email' LIMIT 1";
$result = $link->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$db_password = $row["password"];
if(password_verify($password, $db_password)) echo PHP_EOL . "$password == $db_password";
$error = "";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (!$_POST['email']) {
$error .= "Email required <br>";
}
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email address is invalid.<br>";
}
if (!$_POST['password']) {
$error .= "Password required";
} else {
$password = $_POST['password'];
$email = $link->real_escape_string($_POST['email']);
$sql = "SELECT password, userID FROM `users` WHERE email = '$email' LIMIT 1";
$result = $link->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
//rest of code here
$stmt = $link->prepare("INSERT INTO `users` (email, password, firstName, identifier) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $email, $password, $name, $identifier);
$email = htmlentities($_POST['email'], ENT_QUOTES);
$password = trim(password_hash($password, PASSWORD_BCRYPT, [12]));
$name = htmlspecialchars($_POST['name']);
$identifier = bin2hex($identifier);
$stmt->execute();
$stmt->close();
$password = trim(password_hash($password, PASSWORD_BCRYPT, [12]));
$password = trim(password_hash($_POST['password'], PASSWORD_BCRYPT, [12]));
<?php // demo/temp_blacksulfur.php
/**
* https://www.experts-exchange.com/questions/28966548/password-verify-issues.html
*
* READ THE USER-CONTRIBUTED NOTES CAREFULLY!
* http://php.net/manual/en/function.password-hash.php
* http://php.net/manual/en/function.password-verify.php
*
* References for PHP and MySQL(i):
*
* http://php.net/manual/en/mysqli.overview.php
* http://php.net/manual/en/class.mysqli.php
* http://php.net/manual/en/class.mysqli-stmt.php
* http://php.net/manual/en/class.mysqli-result.php
* http://php.net/manual/en/class.mysqli-warning.php
* http://php.net/manual/en/class.mysqli-sql-exception.php <-- DID NOT WORK PHP 5.3+, MySQL 5.1+
*
* http://php.net/manual/en/mysqli.construct.php
* http://php.net/manual/en/mysqli.real-escape-string.php
* http://php.net/manual/en/mysqli.query.php
* http://php.net/manual/en/mysqli.errno.php
* http://php.net/manual/en/mysqli.error.php
* http://php.net/manual/en/mysqli.insert-id.php
*
* http://php.net/manual/en/mysqli-result.num-rows.php
* http://php.net/manual/en/mysqli-result.fetch-array.php <-- DO NOT USE THIS
* http://php.net/manual/en/mysqli-result.fetch-object.php
*/
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';
// TEST AGAINST THIS PASSWORD
$vwd = 'secret';
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";
require_once('RAY_live_data.php');
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
$err
= "CONNECT FAIL: "
. $mysqli->connect_errno
. ' '
. $mysqli->connect_error
;
trigger_error($err, E_USER_ERROR);
}
// ACTIVATE THIS TO SHOW WHAT THE DB CONNECTION OBJECT LOOKS LIKE
// var_dump($mysqli);
// CREATING A TABLE FOR OUR TEST DATA
$sql
=
"
CREATE TEMPORARY TABLE my_table
( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, pwd VARCHAR(255) NOT NULL DEFAULT ''
, xwhen TIMESTAMP NOT NULL
)
"
;
// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res = $mysqli->query($sql))
{
$err
= 'QUERY FAILURE:'
. ' ERRNO: '
. $mysqli->errno
. ' ERROR: '
. $mysqli->error
. ' QUERY: '
. $sql
;
trigger_error($err, E_USER_ERROR);
}
// ACTIVATE THIS TO SHOW THE RESULTS OF THE QUERY
// var_dump($res);
// LOADING OUR VERIFICATION DATA INTO THE TABLE
$pwd = password_hash($vwd, PASSWORD_DEFAULT);
$pwd = $mysqli->real_escape_string($pwd);
$sql = "INSERT INTO my_table ( pwd ) VALUES ( '$pwd' )";
$res = $mysqli->query($sql);
if (!$res)
{
$err
= 'QUERY FAILURE:'
. ' ERRNO: '
. $mysqli->errno
. ' ERROR: '
. $mysqli->error
. ' QUERY: '
. $sql
;
trigger_error($err, E_USER_ERROR);
}
// ACTIVATE THIS TO SHOW THE QUERY
// echo PHP_EOL . $sql;
// INTERPRET THE URL ARGUMENT AND TRY THE QUERY
$arg = !empty($_GET['q']) ? $_GET['q'] : 'Missing "q=" URL parameter';
$sql = "SELECT id, pwd FROM my_table WHERE id=1 LIMIT 1";
$res = $mysqli->query($sql);
$num = $res->num_rows;
if ($num)
{
$row = $res->fetch_object();
if (password_verify($arg, $row->pwd))
{
echo PHP_EOL . "Found PW match for <b>$arg</b> in row with id=$row->id";
}
else
{
echo PHP_EOL . "Found no PW match for <b>$arg</b>";
}
}
HTH, ~Ray
Also, I can't find any way to tell password_verify() about the parameters used in creating the password_hash() so it may only work with the "vanilla" hash strings. Might be worth testing that!