嗨我有这个页面更改密码。我想检查匹配到数据库中的当前密码。然后保存新密码。但是我在检查数据库的旧密码中进行了比较。我想知道当我点击更改密码按钮时,会出现一个弹出对话框,显示确认密码更改,我不知道弹出对话框来自哪里。而且我的saveNewPass代码也不起作用。有人能帮我解决这个问题吗?以下是我的代码。
我的控制器
<?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');
}
}
我的模特
<?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;
}
}
}
和我的观点
<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>
非常感谢任何帮助
答案 0 :(得分:4)
尝试使用以下代码检查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
不匹配,则上述函数将返回{{ 1}}
编辑:
0
要检查密码和确认密码,请使用JavaScript / Jquery。
JavaScript:
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;
}
答案 1 :(得分:1)
public function changePassword()
{
$pass=$this->input->post('old_password');
$npass=$this->input->post('newpassword');
$rpass=$this->input->post('re_password');
if($npass!=$rpass){
return "false";
}else{
$this->db->select('*');
$this->db->from('users');
$this->db->where('email',$this->session->userdata('admin_email'));
$this->db->where('password',md5($pass));
$query = $this->db->get();
if($query->num_rows()==1){
$data = array(
'password' => md5($npass)
);
$this->db->where('email', $this->session->userdata('admin_email'));
$this->db->update('users', $data);
return "true";
}else{
return "false";
}
}
}