Salam blogger kali ini saya akan coba men-gshare cara merubah password user/admin dengan menggunakan verifikasi password lama di Codeigniter. Tentunya dengan adanya verifikasi dengan password yang lama akan lebih aman lgi.
Controller :
public function save_password()Model :
{
$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('layout/header');
$this->load->view('account/change-pass');
$this->load->view('layout/footer');
}else{
$cek_old = $this->model_account->cek_old();
if ($cek_old == False){
$this->session->set_flashdata('error','Old password not match!' );
$this->load->view('layout/header');
$this->load->view('account/change-pass');
$this->load->view('layout/footer');
}else{
$this->model_account->save();
$this->session->sess_destroy();
$this->session->set_flashdata('error','Your password success to change, please relogin !' );
$this->load->view('layout/header');
$this->load->view('layout/login_sign_up');
$this->load->view('layout/footer');
}//end if valid_user
}
}
public function save()
{
$pass = md5($this->input->post('new'));
$data = array (
'usr_password' => $pass
);
$this->db->where('usr_id', $this->session->userdata('usr_id'));
$this->db->update('users', $data);
}
fungsi untuk mengecek password lama :View :
public function cek_old()
{
$old = md5($this->input->post('old')); $this->db->where('usr_password',$old);
$query = $this->db->get('users');
return $query->result();;
}
<div class="info_contact">Semoga bermanfaat terima kasih.
<h4>Change password regularly</h4>
<div class="content">
<form method="post" action="<?=site_url();?>change-password-save" >
<table class="input"><tr><td>Old Password</td><tr>
<tr><td class="input-width"><input type="password" name="old" value="<?php echo set_value('old');?>" required></td></tr>
<tr><td>New Password</td><tr>
<tr><td class="input-width"><input type="password" name="new" value="<?php echo set_value('new');?>" required></td></tr>
<tr><td>Re-type New Password</td><tr>
<tr><td class="input-width"><input type="password" name="re_new" value="<?php echo set_value('re_new'); ?>" required></td></tr>
<tr><td>
<button type='submit' class='btn1' value='' >Save</button>
<button type='text' class='btn2' value='' ><a href="<?=base_url('customer/account')?>">Cancel</a></button>
</td></tr>
</table>
</form>
</div>
<div class="error">
<?= validation_errors() ?>
<?= $this->session->flashdata('error') ?>
</div>
</div>
Thanks alot it worked!!!
ReplyDeleteyou welcome, may be useful
DeleteThanks for sharing this blog its very informative and useful for us.
ReplyDeleteหนังออนไลน์
You welcome,
Deletetable pass ny ada 2 atau satu gan? old sama usr_password?
ReplyDelete1 tabel gan, old itu cuma nama di input text di view, sedangkan usr_password nama field di database
Deletemau tanya dong kalo set_rules di controller ini buat apa ya?
ReplyDelete$this->form_validation->set_rules('new','New','required|alpha_numeric');
$this->form_validation->set_rules('re_new', 'Retype New', 'required|matches[new]');
harus diisi mas.
Deletesudah dicoba tpi muncul error gan. kenapa yaa? errornya kaya gini :
ReplyDelete[
An uncaught Exception was encountered
Type: Error
Message: Call to a member function cek_old() on null
]
[Message: Undefined property: data::$M_Data
Filename: controllers/data.php]
coba cek modelnya sudah sesuai kah ?
Delete