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