2017-06-29 54 views
-5

我使用的是PHP 7.1.6。我正在嘗試使用password_hash,但似乎沒有工作。這裏是我的代碼:Password_hash沒有散列

$post = $this->input->post(); 
$maxid = $this->db->query('SELECT MAX(App_Users.ID) AS MAXID FROM App_Users')->row()->MAXID; 
$maxid = $maxid + 1; 
$hash = password_hash($post['Password'], PASSWORD_DEFAULT); 
$this->db->query("INSERT INTO APP_USERS (ID, NAME, PASSWORD) 
VALUES(".$maxid.", '".$post['Name']."', '". $hash ."')"); 

密碼存儲沒有散列。 $ post ['Password'] ='1234',它被存儲在數據庫中'1234'。

我做錯了什麼?

+1

'$ post'是從哪裏來的? –

+0

請var_dump $ post – MorganFreeFarm

+1

'$ hash'是一個字符串,不是一個整數 - 編輯:一旦'password_hash()'踢入。 –

回答

-1
//this is normal way to generate has password// 
$hash = password_hash("input value", PASSWORD_DEFAULT); 
$this->db->query("INSERT INTO APP_USERS (ID, NAME, PASSWORD) 
VALUES(".$maxid.", '".$post['Name']."', '". $hash ."')"); 

//In this way we can provide cost and salt to generate hash value// 

$options = [ 
'cost' => 11, 
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM), 
]; 
$hash = password_hash("input value", PASSWORD_BCRYPT, $options); 

輸入值,你通過$ post ['哪個值你想散列']。

希望它對你有所幫助

+0

建議不要提供鹽,而是讓'password_hash'創建salt。同樣在PHP 7.x中,salt選項已被刪除。參見[password_hash](http://php.net/manual/en/function.password-hash.php),「如果省略,每次密碼散列都會通過password_hash()產生一個隨機salt。這是預期的模式的操作。「 – zaph