我對password_verify 沒有什么問題。我使用 codeigniter 3 進行學習。我有用戶模型<?phpclass User_model extends CI_Model{ public function __construct() { parent::__construct(); $this->load->database(); } public function createUser($args): int { $query = $this->db->insert('user', $args); return $this->db->affected_rows(); } public function getUser(string $email): array { $query = $this->db->get_where('user',array('email'=> $email)); return $query->row_array(); }}用戶控制器<?phpclass UserController extends CI_Controller{ public function __construct() { parent::__construct(); $this->load->model('user_model'); $this->load->helper('url_helper'); } public function profile() { $data['user'] = $this->user_model->getUser($_POST['email']); $hash = $data['user']['password']; if(password_verify($_POST['password'],$hash)){ echo "success"; }else{ echo "failed"; } $this->load->view('templates/header'); $this->load->view('pages/profileUser',$data); }如果我在控制器中使用靜態密碼,它可以工作,如果我通過數據庫函數password_verify 獲取哈希密碼,則它不起作用。謝謝你的支持,
1 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
正如 php.net所解釋的?password_verify()
那樣,驗證通過password_hash()
.
如果您使用 sha256 哈希,您可以更好地對用戶輸入進行哈希處理并與數據庫查詢的密碼進行比較,如下所示:
$password = $this->input->post('password');
if(hash('sha256', $password) == $hash) {
?echo 'success';
} else {
?echo 'failed';
}
我認為更好的方法是在查詢中動態檢查密碼,如下所示:
$user_query = $this->db->get_where('users', array('email' => $email, 'password' => hash('sha256', $password));
if($user_query->num_rows() == 0) {
?print 'failed';
} else {
?print 'success';
?$user = $user_query->row_array();
?...
}
順便說一句:最好使用 CodeIgniters $this->input->post('password') 因為它會為您進行安全過濾。
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消