2017-06-29 59 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