2015-10-18 48 views
-1

通常你會在php中編寫一個sql語句就像這樣查詢mysql數據庫。我如何在php中使用password_verify查詢mysql數據庫

$sql="select * from table where userid='username' and password='$pass'"; 

我的問題是,如果我使用password_hash($通,PASSWORD_BCRYPT)來加密我的密碼,我怎樣寫上面的查詢檢索從我的數據庫中的某些行,因爲哈希密碼存儲在數據庫將不會是由用戶輸入的相同的文本密碼? 請指教。謝謝。

+0

出了什麼問題,我的問題? – schenker

+0

可能重複[在哪裏把密碼\ _verify登錄腳本?](http://stackoverflow.com/questions/32554085/where-to-put-password-verify-in-login-script) –

回答

0

對於存儲密碼進入數據庫

<?php 
/** 
* Note that the salt here is randomly generated. 
* Never use a static salt or one that is not randomly generated. 
* 
* For the VAST majority of use-cases, let password_hash generate the salt randomly for you 
*/ 
$options = [ 
    'cost' => 11, 
    'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM), 
]; 
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n"; 
?> 

,如果你想使用password_verify()函數驗證密碼。

首先提取數據庫存儲的密碼,並使用password_verify()函數

<?php 
// See the password_hash() example to see where this came from. 
$hash = '$2y$11$lBi3B5rakkB6CBJRLn2e6O1RppUr2y5r0W/4Z0jJBGqE9cdYK.1sa'; 

if (password_verify('rasmuslerdorf', $hash)) { 
    echo 'Password is valid!'; 
} else { 
    echo 'Invalid password.'; 
} 

?> 

Click HERE TO CHECK