2011-11-25 87 views
0

我有一個儀表板,其中包含可以編輯的用戶詳細信息。我想添加一個密碼更改,但他們可以更改密碼之前,我將如何驗證其當前的密碼,然後讓他們更改爲一個新的密碼?在更改爲新密碼之前確認舊密碼codeigniter

所以窗體將有3個字段。第一場將有current_password,其後跟new_passwordconfirm_password

回答

1

笨配備了表單驗證類你可以找到documentation here。 它的目的正是它的名字 - 它會幫助你驗證你的表單輸入。一旦你習慣了它,它真的很方便。

這是怎麼控制器可以看看:

public function change_password() { 
    if ($this->input->post()) { 
     // user submitted the form 
     if (some_encryption_function($this->input->post('current_password'))==password_from_db) { // pseudo code 

     $this->load->library('form_validation'); // this should probably belong somewhere else like in the constructor of the controller 
     $this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[4]|max_length[12]|matches[confirm_password]'); 
     $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[4]|max_length[12]'); 

     if ($this->form_validation->run() == false) { 
      data['message'] = validation_errors(); 
     } else { 
      store_new_password_to_db($this->input->post('new_password'); 
      data['message'] = "Some success message"; 
     } 
     $this->load->view('your_change_password_view',$data); 
     } 
    } 
} 

這不是表單驗證類一個很好的例子。僅僅因爲你可以在沒有幫助的情況下輕鬆驗證這三個域。但是,它已經與codeigniter建立在一起,爲什麼不使用它?

0

讓他們輸入舊密碼,新密碼並確認新密碼。然後,

if (old pass == password stored in database) 
{ 
    if (new password == confirm password) 
    { 
      //update password in database 
    } 
} 
0
 
$oldPass = get_password_from_db(); 
$currentPass = $this->input->post("old_pass"); 
$newPass = $this->input->post("new_pass"); 
$confPass = $this->input->post("conf_pass"); 
//check if new and confirm pass are same 
if(md5($currentPass) == $oldPass) { 
    //update query to change to new pass 
} 

希望它可以幫助

+0

謝謝你的幫助。它正指向我正確的方向! – Wolfwood