Change password with verification old password in Codeigniter

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()
{
$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
}
}
Model :

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 :
public function cek_old()
{
$old = md5($this->input->post('old')); $this->db->where('usr_password',$old);
$query = $this->db->get('users');
return $query->result();;
}
View :
<div class="info_contact">
<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>
Semoga bermanfaat terima kasih.
Share:

10 comments :

  1. Thanks for sharing this blog its very informative and useful for us.

    หนังออนไลน์

    ReplyDelete
  2. table pass ny ada 2 atau satu gan? old sama usr_password?

    ReplyDelete
    Replies
    1. 1 tabel gan, old itu cuma nama di input text di view, sedangkan usr_password nama field di database

      Delete
  3. mau tanya dong kalo set_rules di controller ini buat apa ya?

    $this->form_validation->set_rules('new','New','required|alpha_numeric');
    $this->form_validation->set_rules('re_new', 'Retype New', 'required|matches[new]');

    ReplyDelete
  4. sudah dicoba tpi muncul error gan. kenapa yaa? errornya kaya gini :
    [
    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]

    ReplyDelete