如何检查旧密码并将其更新为新密码?

时间:2017-02-16 04:47:19

标签: php codeigniter

我的代码中没有发生任何事情。没有显示错误,但密码没有变化。点击提交按钮后,它只会重定向到password_view。

这是我的密码视图:password_view.php

<?php echo form_open_multipart('password');?>

    <div class="form-group">
        <label class="control-label col-lg-2"> Old Password:</label>
            <div class="col-lg-4">
                <input class="form-control" name="oldPassword" placeholder="Old Password" type="password">
                <span class="text-danger"><?php echo form_error('oldPassword'); ?></span>
            </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2"> New Password:</label>
            <div class="col-lg-4">
                <input class="form-control" name="newPassword" placeholder="New Password" type="password">
                <span class="text-danger"><?php echo form_error('newPassword'); ?></span>
            </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2"> Re-type Password:</label>
            <div class="col-lg-4">
                <input class="form-control" name="renewPassword" placeholder="Re-type Password" type="password">
                <span class="text-danger"><?php echo form_error('renewPassword'); ?></span>
            </div>
    </div>

    <div class="form-group">
        <label class="control-label col-lg-2"></label>
            <div class="col-lg-4">
                <button type="submit" class="btn btn-primary btn-grad btn-rect">Submit</button>
                <a href ="<?php echo base_url();?>index.php/main" class="btn btn-default btn-grad btn-rect">Cancel</a>
            </div>
    </div>

<?php echo form_close(); ?>

这是我的密码控制器:password.php

public function index()
{
    $this->form_validation->set_rules('oldPassword','Old Password','trim|required|min_length[5]|max_length[30]');
    $this->form_validation->set_rules('newPassword','New Password','trim|required|min_length[5]|max_length[30]');
    $this->form_validation->set_rules('renewPassword','Retype Password','trim|required|matches[newPassword]');

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('password_view');
    }
    else
    {
        $this->load->model('account_model');
        $this->load->view('password_view');
        $query = $this->account_model->checkOldPassword($this->input->post('oldPassword'));

        if($query)
        {
            $query = $this->account_model->saveNewPassword($this->input->post('newPassword'));

            if($query)
            {
                $this->session->sess_destroy();
                $data['error']='Password Change Successfully. Please login to Continue.';
                $this->load->view('password_view',$data);
            }
            else
            {
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('account/editPassword');
            }
        }
    }
}

这是我的密码模型:account_model.php

public function checkOldPassword($oldPassword)
{
    $this->db->select('AID');
    $this->db->where('AID',$this->session->userdata('id'));
    $this->db->where('Password',$this->input->post('oldPassword'));
    $query=$this->db->get('tblaccount');
    if($query->num_rows>0)
    {
        $row=$query->row();

        if($oldPassword == $row->Password)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public function saveNewPassword($newPassword)
{
    $data = array(
            'Password' => $newPassword
            );

    $this->db->where('AID', $this->session->userdata('id'));
    $this->db->where('Password',$this->input->post('oldPassword'));
    $query = $this->db->update('tblaccount', $data);

    if($query)
    {
        return true;
    }
    else
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

旧密码规则的更改

oldPassword 字段中添加 callback_checkOldPassword 规则

$this->form_validation->set_rules('oldPassword','Old Password','trim|required|min_length[5]|max_length[30]|callback_checkOldPassword');

checkOldPassword方法的变化

checkOldPassword 规则

设置错误消息
public function checkOldPassword($oldPassword)
{
    $this->db->select('AID');
    $this->db->where('AID',$this->session->userdata('id'));
    $this->db->where('Password',$this->input->post('oldPassword'));
    $query=$this->db->get('tblaccount');
    if($query->num_rows>0)
    {
        return true;
    }else{
        $this->form_validation->set_message('checkOldPassword', 'wrong old password.');
        return false;
    }