我想问一下如何更改登录用户的密码, 当我输入与数据库中的任何人匹配的密码时,我可以更改密码。 例如,用户有“admin”密码,我只需输入当前密码, 新密码和确认密码。
当前密码:admin 新密码:newadmin 当前密码:新管理员
而且如果密码我也不知道如何更改密码 使用md5()。我希望你能帮助我,我是Codeigniter的新手。 我搜索答案,但我真的不明白,所以我想评论但是 它需要50个声誉,所以我发布了新的问题。
这是我的代码:
控制器
public function update(){
$this->form_validation->set_rules('password', 'Current Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('newpass', 'New Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('confpassword', 'Confirm Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
if($this->form_validation->run()){
$cur_password = $this->input->post('password');
$new_password = $this->input->post('newpass');
$conf_password = $this->input->post('confpassword');
$this->load->model('queries');
$userid = '1';
$passwd = $this->queries->getCurrPassword($userid);
if($passwd->password == $cur_password){
if($new_password == $conf_password){
if($this->queries->updatePassword($new_password, $userid)){
echo 'Password updated successfully';
}
else{
echo 'Failed to update password';
}
}
else{
echo 'New password & Confirm password is not matching';
}
}
else{
echo'Sorry! Current password is not matching';
}
}
else{
echo validation_errors();
}
模型
public function getCurrPassword($userid){
$query = $this->db->where(['id'=>$userid])
->get('users');
if($query->num_rows() > 0){
return $query->row();
} }
public function updatePassword($new_password, $userid){
$data = array(
'password'=> $new_password
);
return $this->db->where('id', $userid)
->update('users', $data); }
答案 0 :(得分:1)
我找到了解决问题的方法。
对于已登录的用户我刚刚更改了$ userid ='1';到 $ userid = $ this-> session-> userdata('account_id');
对于md5密码 我只是在密码上添加md5。就像@sintakonte所做的那样,@ zaph是对的。
“只使用像BCrypt这样的强密码哈希算法,这在PHP自己的密码哈希函数中使用。”
参考:https://www.codeigniter.com/userguide3/general/security.html
感谢帮助人员!
答案 1 :(得分:-2)
我不打算在这里讨论md5主题,但你应该避免那些弱算法,因为它们不安全。为此,请使用password_verify
和password_hash
。 (但正如我所说,我不是传教士)
你需要更好地组织你的代码 - 因为这是一团糟;)
尝试以下操作 - 您的控制器
public function update()
{
$this->form_validation->set_rules('password', 'Current Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('newpass', 'New Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
$this->form_validation->set_rules('confpassword', 'Confirm Password', 'required|alpha_numeric|min_length[6]|max_length[20]');
if($this->form_validation->run())
{
$cur_password = $this->input->post('password');
$new_password = $this->input->post('newpass');
$conf_password = $this->input->post('confpassword');
$this->load->model('queries');
$userid = '1';
try
{
$objUser = $this->queries->getUser($userid);
if ($objUser->password != md5($cur_password)) throw new Exception('Sorry! Current password is not matching');
if ($new_password != $conf_password) throw new Exception('New password & Confirm password is not matching');
$this->queries->updatePassword($new_password, $userid);
echo 'Password updated successfully';
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
else
{
echo validation_errors();
}
}
和模型
public function getUser($userid)
{
$query = $this->db->where(id,$userid])->get('users');
if($query->num_rows() == 1)
{
return $query->row();
}
throw new Exception("no user data found");
}
public function updatePassword($new_password, $userid)
{
$data = array
(
'password'=> md5($new_password)
);
if (!$this->db->where('id', $userid)->update('users', $data))
{
throw new Exception('Failed to update password');
}
}
如果它实际上是返回一个用户对象,则无需为模型函数getCurrPassword
命名 - 所以我将其重命名。