检查旧密码并使用codeigniter更新新密码 [英] check old password and updating a new password using codeigniter

查看:162
本文介绍了检查旧密码并使用codeigniter更新新密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有此页更改密码。我想检查与数据库匹配的当前密码。然后保存新密码。但是我得到了它在旧密码比较检查数据库。我想知道当我单击更改密码按钮有一个弹出对话框,说确认密码更改,我不知道哪里弹出对话框来自。还有我的saveNewPass代码将不工作。有人可以帮助我想出这一点吗?这是我的代码如下。



我的控制器

 <? php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

class Change_password extends CI_Controller {
public function __construct(){
parent :: __ construct();
$ this-> load-> library('form_validation');
$ this-> load-> model('users_model','um');
$ this-> load-> library('session');
if(!$ this-> session-> userdata('loggedIn')){
redirect('login');
}
}




public function change(){
$ this-> form_validation-> set_rules ('old_password','Password','trim | required | xss_clean');
$ this-> form_validation-> set_rules('newpassword','New Password','required | matches [re_password]');
$ this-> form_validation-> set_rules('re_password','重新输入密码','必填');
if($ this-> form_validation-> run()== FALSE){
$ this-> data ['title'] ='更改密码'
$ sessionData = $ this-> session-> userdata('loggedIn');
$ this-> data ['id'] = $ sessionData ['id'];
$ this-> data ['username'] = $ sessionData ['username'];
$ this-> data ['type'] = $ sessionData ['type'];

$ this-> load-> view('templates / header',$ this-> data);
$ this-> load-> view('pages / change_password');
$ this-> load-> view('templates / footer');
} else {
$ query = $ this-> um-> checkOldPass(s​​ha1($ this-> input-> post('old_password'))));
if($ query){
$ query = $ this-> um-> saveNewPass(s​​ha1($ this-> input-> post('newpassword')));
if($ query){
redirect('change_password');
} else {
redirect('change_password');
}
}

}
}

public function index(){
$ this-> data [' title'] ='更改密码';
$ sessionData = $ this-> session-> userdata('loggedIn');
$ this-> data ['id'] = $ sessionData ['id'];
$ this-> data ['username'] = $ sessionData ['username'];
$ this-> data ['type'] = $ sessionData ['type'];

$ this-> load-> view('templates / header',$ this-> data);
$ this-> load-> view('pages / change_password');
$ this-> load-> view('templates / footer');
}
}

我的模型

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

class Users_model extends CI_Model {


public function checkOldPass($ old_password){
$ this-> db-> where ',$ this-> session-> userdata('username'));
$ query = $ this-> db-> get('users');
$ row = $ query-> row();
echo旧密码:。$ old_password。< br>;
echoFrom DB:。$ row-> password。< br>;
die;

if($ query-> num_rows> 0){
$ row = $ query-> row();
if($ old_password == $ row-> password){
return true;
} else {
return false;
}
}
}

public function saveNewPass($ new_pass){
$ array = array(
'password'=> $ new_pass
);
$ this-> db-> where('username',$ this-> session-> userdata('username'));
$ query = $ this-> db-> update('users');
if($ query){
return true;
} else {
return false;
}
}



}


b $ b

和我的视图

 < div class =container 
< div class =homepage_content>
< div>
< ul class =nav nav-pills>
< li>< a href =>首页< / a>< / li>
< li>< a href =>查看个人资料< / a>< / li>
< li>< a href =>编辑个人资料< / a>< / li>
< li>< a href =<?php echo base_url()。'change_password'?>>更改密码< / a&
<?php if($ type ==Root):?>
< li>< a href =>创建新帐户< / a>< / li>
<?php endif; >
< li class =pull-right>< a class =btn btn-primaryhref =<?php echo base_url()。'logout'?& / a>< / li>
< / ul>
< / div>
< br />
< br />
< p> welcome<?php echo $ username; ?>< / p>
< p>帐户类型<?php echo $ type; ?>< / p>
< br />
< form action =<?php echo base_url()。'change_password / change'?> method =post>
< div class =errors>
<?php echo validation_errors('< div class =error>','< / div>'); >
< / div>
< p>< input type =passwordname =old_passwordplaceholder =Current Password:class =form-control>< / p>< br /
< p>< input type =passwordname =newpasswordplaceholder =New Password:class =form-control>< / p>< br /
< p>< input type =passwordname =re_passwordplaceholder =Retype New Password:class =form-control>< / p>< br /
< input type =submitvalue =change passwordclass =btn btn-primary/>
< / form>
< / div>
< / div>

任何帮助非常感谢。

解决方案

尝试以下代码以检查 username old_password 是否匹配,

  public function checkOldPass($ old_password)
{
$ id = $ this-> input-> post 'ID');
$ this-> db-> where('username',$ this-> session-> userdata('username'));
$ this-> db-> where('id',$ id);
$ this-> db-> where('password',$ old_password);
$ query = $ this-> db-> get('users');
if($ query-> num_rows()> 0)
return 1;
else
return 0;
}

如果 username old_password 匹配上述函数将返回 1 和如果 username old_password 不匹配上述函数将返回 0



编辑:

  public function saveNewPass($ new_pass)
{
$ data = array
'password'=> $ new_pass
);
$ this-> db-> where('id',$ this-> input-> post('id'))
$ this-> db-> update('users',$ data);
return true;
}

要检查密码和确认密码,请使用JavaScript / Jquery。

JavaScript:

  function chkPwd(a)
{
// let Pswd是密码的ID,cPswd是确认密码文本的框ID
var newPwd = document.getElementById('Pswd')。value;
var cPwd = document.getElementById('cPswd')。value;
if(newPwd!= cPwd)
{
document.getElementById('cPswd')。focus();
document.getElementById('cPswd')。value =;
document.getElementById('err')。innerHTML =密码不匹配;
}
}


Hi i have this page change password. I want to check the current password that matches into the database. then saving the new password on it. However i got it compared in the old password that checks on the database. Im wondering that when i click the change password button there is a pop up dialog box that says confirm password change, and i dont know where that pop up dialog came from. And also my saveNewPass code will not work. Can someone help me figured this out?. Here is my code below.

My controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Change_password extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->model('users_model', 'um');
    $this->load->library('session');
    if(!$this->session->userdata('loggedIn')){
      redirect('login');
    }
  }




  public function change(){
    $this->form_validation->set_rules('old_password', 'Password', 'trim|required|xss_clean');
    $this->form_validation->set_rules('newpassword', 'New Password', 'required|matches[re_password]');
    $this->form_validation->set_rules('re_password', 'Retype Password', 'required');
    if($this->form_validation->run() == FALSE){
       $this->data['title'] = 'Change Password';
        $sessionData = $this->session->userdata('loggedIn');
        $this->data['id'] = $sessionData['id'];
        $this->data['username'] = $sessionData['username'];
        $this->data['type'] = $sessionData['type'];

        $this->load->view('templates/header', $this->data);
        $this->load->view('pages/change_password');
        $this->load->view('templates/footer');
    }else{
      $query = $this->um->checkOldPass(sha1($this->input->post('old_password')));
      if($query){
        $query = $this->um->saveNewPass(sha1($this->input->post('newpassword')));
        if($query){
          redirect('change_password');
        }else{
          redirect('change_password');
        }
      }

    }
  }

  public function index(){
    $this->data['title'] = 'Change Password';
    $sessionData = $this->session->userdata('loggedIn');
    $this->data['id'] = $sessionData['id'];
    $this->data['username'] = $sessionData['username'];
    $this->data['type'] = $sessionData['type'];

    $this->load->view('templates/header', $this->data);
    $this->load->view('pages/change_password');
    $this->load->view('templates/footer');
  }
}

My model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model {


   public function checkOldPass($old_password){
    $this->db->where('username', $this->session->userdata('username'));
    $query = $this->db->get('users');
    $row    = $query->row();
    echo "Old Password : ".$old_password."<br>";
    echo "From DB : ".$row->password."<br>";
    die;

    if($query->num_rows > 0){
      $row = $query->row();
      if($old_password == $row->password){
        return true;
      }else{
        return false;
      }
    }
  }

  public function saveNewPass($new_pass){
    $array = array(
            'password'=>$new_pass
            );
    $this->db->where('username', $this->session->userdata('username'));
    $query = $this->db->update('users');
    if($query){
      return true;
    }else{
      return false;
    }
  }  



}

and my views

<div class="container">
  <div class="homepage_content">
    <div>
      <ul class="nav nav-pills">
        <li><a href="">Home</a></li>
        <li><a href="">View Profile</a></li>
        <li><a href="">Edit Profile</a></li>
        <li><a href="<?php echo base_url().'change_password'?>">Change Password</a></li>
        <?php if($type == "Root"): ?>
        <li><a href="">Create New Account</a></li>
        <?php endif; ?>
        <li class="pull-right"><a class="btn btn-primary" href="<?php echo base_url().'logout'?>">Logout</a></li>
      </ul>
    </div>
    <br />
    <br />
    <p>welcome <?php echo $username; ?></p>
    <p>Account Type <?php echo $type; ?></p>
    <br />
    <form action="<?php echo base_url().'change_password/change'?>" method="post">
      <div class="errors">
        <?php echo validation_errors('<div class="error">', '</div>'); ?>
      </div>
      <p><input type="password" name="old_password" placeholder="Current Password: " class="form-control"></p><br />
      <p><input type="password" name="newpassword" placeholder="New Password: " class="form-control"></p><br />
      <p><input type="password" name="re_password" placeholder="Retype New Password: " class="form-control"></p><br />
      <input type="submit" value="change password" class="btn btn-primary" />
    </form>
  </div>
</div>

Any help is muchly appreciated

解决方案

try following code to check username and old_password are matching,

public function checkOldPass($old_password)
{
    $id = $this->input->post('id');
            $this->db->where('username', $this->session->userdata('username'));
            $this->db->where('id', $id);
    $this->db->where('password', $old_password);
    $query = $this->db->get('users');
    if($query->num_rows() > 0)
        return 1;
    else
        return 0;
}

if username and old_password are matching the above function will return 1 and if username and old_password are not matching the above function will return 0

EDIT :

public function saveNewPass($new_pass)
{
    $data = array(
           'password' => $new_pass
        );
    $this->db->where('id', $this->input->post('id'));
    $this->db->update('users', $data);
    return true;
}

To check Password and Confirm Password, use JavaScript/ Jquery.

JavaScript :

function chkPwd(a)
    {
        // let Pswd is ID of password and cPswd is ID of confirm password text Box
        var newPwd = document.getElementById('Pswd').value;
        var cPwd = document.getElementById('cPswd').value;
        if(newPwd != cPwd)
        {
            document.getElementById('cPswd').focus();
            document.getElementById('cPswd').value="";
            document.getElementById('err').innerHTML="Passwords are Not Matching";
        }   
    }

这篇关于检查旧密码并使用codeigniter更新新密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆