-2
散列我的密碼(b-crypt)有問題。用戶註冊成功完成此功能:$hashed = password_hash($password, PASSWORD_DEFAULT);
。但我不知道如何實現這一行代碼password_verify($password, $hashed_password)
我的代碼。Password_verify,PDO
這是一類用戶的一個片段:
class User{
private $db;
public function __construct() {
$this->db = new Database();
}
public function getLoginUser($email, $password){
$sql = "SELECT * FROM tbl_user WHERE email = :email AND password = :password LIMIT 1";
$query = $this->db->pdo->prepare($sql);
$query->bindValue(':email', $email);
$query->bindValue(':password', $password);
$query->execute();
$result = $query->fetch(PDO::FETCH_OBJ);
return $result;
}
public function userLogin($data){
$email = $data['email'];
$password = $data['password'];
$chk_email = $this->emailCheck($email);
if ($email == "" OR $password == "") {
$msg = "<div class='alert alert-danger'><strong>Error! </strong>Field must not be Empty!</div>";
return $msg;
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$msg = "<div class='alert alert-danger'><strong>Error! </strong>The E-mail address is not valid!</div>";
return $msg;
}
if ($chk_email == false) {
$msg = "<div class='alert alert-danger'><strong>Error! </strong>The E-mail address Not Exist!</div>";
return $msg;
}
if (password_verify('password', $hashed)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
$result = $this->getLoginUser($email, $password);
if ($result) {
Session::init();
Session::set("login", true);
Session::set("id", $result->id);
Session::set("name", $result->name);
Session::set("username", $result->username);
Session::set("loginmsg", "<div class='alert alert-success'><strong>Success! </strong>You are Logged In!</div>");
header("Location: index.php");
} else {
$msg = "<div class='alert alert-danger'><strong>Error! </strong>Data not found!</div>";
return $msg;
}
}
,這是我的login.php與功能USERLOGIN($ _ POST); 。
<?php
$user = new User();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) {
$usrLogin = $user->userLogin($_POST);
}
有人可以幫我解決這個問題嗎?如果你能爲我寫這段代碼(段),這將會很有幫助。
謝謝
Reegards!
'$ hashed'是定義在哪裏以及如何?密碼列的長度是多少? –
你的問題是,SQL語句檢查密碼,但這是不可能的,因爲密碼哈希被鹽漬,因此無法搜索。我試圖解釋在另一個[回答](http://stackoverflow.com/a/38422760/575765)正確的處理。 – martinstoeckli