3 回答

TA貢獻1836條經(jīng)驗 獲得超4個贊
我認為該userLogin函數(shù)應該返回一個值(true / false),而不管密碼是否匹配,以便if / else邏輯起作用。由于返回值為password_verifytrue或false,因此可以簡單地返回。
public function userLogin($username, $password){
$sql='select `password` from `farms` where `farmname` = ?'
$stmt=$this->con->prepare( $sql );
if( !$stmt )return false;
$stmt->bind_param( 's', $username );
$res=$stmt->execute();
if( !$res )return false;
$stmt->store_result();
$stmt->bind_result( $pwd );
$stmt->fetch();
$stmt->free_result();
$stmt->close();
return password_verify( $password, $pwd );
}
--
忙著在車庫里忙碌,但根據(jù)我數(shù)據(jù)庫中的數(shù)據(jù)迅速整理了上述功能的一些演示。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = 'xxx';
$dbname = 'experiments';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
/*
the class from which userLogin originates was unknown so I guessed
and made an ultra basic representation of what it might be.
*/
class user{
private $con;
public function __construct( $con ){
$this->con=$con;
}
public function userLogin($username, $password){
$sql='select `password` from `farms` where `farmname` = ?';
/*
as I do not have a table `farms` I chose another
table that has a hashed password column to test against.
*/
$sql='select `hashpwd` from `users` where `username`=?';
$stmt=$this->con->prepare( $sql );
if( !$stmt )return false;
$stmt->bind_param( 's', $username );
$res=$stmt->execute();
if( !$res )return false;
$stmt->store_result();
$stmt->bind_result( $pwd );
$stmt->fetch();
$stmt->free_result();
$stmt->close();
return password_verify( $password, $pwd );
}
}//end class
/* instantiate the class with the db as an argument */
$user=new user( $db );
/* capture POST vars */
$username=filter_input( INPUT_POST,'username',FILTER_SANITIZE_STRING );
$password=filter_input( INPUT_POST,'password',FILTER_SANITIZE_STRING );
/* test if the password was OK or not... */
if( $user->userLogin($username,$password) ){
echo "OK";
} else {
echo "Bogus";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Farm - Form - mySQLi</title>
</head>
<body>
<form method='post'>
<input type='text' name='username' />
<input type='password' name='password' />
<input type='submit' />
</form>
</body>
</html>
毫不奇怪,結果"OK"表明該功能按預期工作。因此,總而言之,我建議問題出在其他地方

TA貢獻1884條經(jīng)驗 獲得超4個贊
我認為您的功能在if之后需要else語句
例如:
if (password_verify($hash, $password)) {
return $stmt->num_rows > 0;
}
else{}
- 3 回答
- 0 關注
- 205 瀏覽
添加回答
舉報