Postingan lainnya
Cara merubah password dengan validasi password lama di codeigniter
Controller
public function save_password() { $this->form_validation->set_rules('new','New','required|alpha_numeric'); $this->form_validation->set_rules('re_new', 'Retype New', 'required|matches[new]'); if ($this->form_validation->run() == FALSE) { $this->load->view('change-pass'); }else{ $cek_old = $this->Globalmodel->cek_old(); if ($cek_old == FALSE) { $this->session->set_flashdata('error','Old Password Doesnt Match'); $this->load->view('change-pass'); }else{ $this->Globalmodel->save(); $this->session->sess_destroy(); $this->session->set_flashdata('error','Password Successfully Changed , Please Relogin !'); redirect("",'refresh'); } } }
Model
public function save() { $passen=md5($this->input->post('new')); $data=array( 'pass'=>$passen ); $this->db->where('id_user',$this->session->userdata('id_user')); $this->db->update('user',$data); return true; } //Fungsi untuk mengecek password lama public function cek_old() { $old = md5($this->input->post('old')); $this->db->where('pass',$old); $query = $this->db->get('user'); return $query->result(); }
View
Ayolah
Change Password
Old PasswordNew PasswordRe-type New PasswordSaveCancel
session->flashdata('error') ?>
Tanggapan
baca aturan main, cara bertanya yang jelas
coba diperbaiki dulu mas kodingannya ditaruh di tag yang benar.Biar enak di baca.
1 Jawaban:
Mungkin bisa pakai cara saya ini:
pada bagian controller :
<pre>
$cek_old = $this->Globalmodel->cek_old($idUser,$passlama);
if($cek_old == 0){
$this->session->set_flashdata('pesanGagal', "Password lama anda tidak cocok"); redirect('setting/ganti_password');
} else {
$passBaruEncy = md5($this->input->post('old');
$dataUpdatePass = array(
'password' => $passBaruEncy
);
$this->m_user->updatePass($idUser,$dataUpdatePass);
$this->session->set_flashdata('pesanSukses', "Password Berhasil dirubah Berhasil dirubah.");
redirect('setting/ganti_password');
}
</pre>
Catatan: (iduser bisa didapat dari session misal $idUser = $this->session->userdata('id_user');)
untuk modelnya: <pre> function cek_old($idUser,$passlama){ $this->db->where('id_user', $idUser); $this->db->where('password', $passlama); $query = $this->db->get('tbuser'); return $query->num_rows(); } function updatePass($idUser,$dataUpdate){ $this->db->where('id_user',$idUser); $this->db->update('tbuser',$dataUpdate); } </pre>