title: Codeigniter User Change Password
body: <p>Hello I am trying to create a user change password feature for the user control panel, and I have found something here tried to edit but I have some problems.</p>
  
  <p>Here is the controller part</p>
  
  <pre><code>public function ucp_change_pass() {
  
      $data2['title'] = "Change Password";  
      $data = new stdClass();
      $this-&gt;form_validation-&gt;set_rules('old_password', 'Old Password', 'trim|required|min_length[4]');
      $this-&gt;form_validation-&gt;set_rules('new_password', 'New Password', 'trim|required|min_length[4]');
      $this-&gt;form_validation-&gt;set_rules('new_password_confirm', 'Old Password', 'trim|required|min_length[4]|matches[new_password]');
      if ($this-&gt;form_validation-&gt;run() === false) {
       $this-&gt;load-&gt;view('header', $data2);
      $this-&gt;load-&gt;view('ucp/ucp_menu');
      $this-&gt;load-&gt;view('ucp/ucp_change_pass', $data);
      $this-&gt;load-&gt;view('footer');
  
  
          } else {    
      $old_password = $this-&gt;input-&gt;post('old_password');
      $new_password_confirm = $this-&gt;input-&gt;post('new_password_confirm');
      $query = $this-&gt;Home_model-&gt;checkOldPass($old_password);
      if($query){
      $query = $this-&gt;Home_model-&gt;saveNewPass($new_password_confirm);
      if($query){
        redirect('./ucp_change_pass_success');
      }else{
        redirect('./ucp_change_pass');
      }
    }
  
   }
  
   }
  </code></pre>
  
  <p>Here is the model part </p>
  
  <pre><code>public function checkOldPass($old_password){
  $this-&gt;db-&gt;where('username', $this-&gt;session-&gt;userdata('username'));
  $query = $this-&gt;db-&gt;get('users');
  $row    = $query-&gt;row();
  echo "Old Password : ".$this-&gt;hash_password($old_password)."&lt;br&gt;";
  echo "From DB : ".$row-&gt;password."&lt;br&gt;";
  die;
  
  if($query-&gt;num_rows &gt; 0){
    $row = $query-&gt;row();
    if($old_password == $row-&gt;password){
      return true;
    }else{
      return false;
     }
   }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public function saveNewPass($new_password_confirm){
  $array = array(
          'password'=&gt;$this-&gt;hash_password($new_password_confirm)
          );
  $this-&gt;db-&gt;where('username', $this-&gt;session-&gt;userdata('username'));
  $query = $this-&gt;db-&gt;update('users');
  if($query){
    return true;
  }else{
    return false;
  }
  }  
  </code></pre>
  
  <p>and this is the view</p>
  
  <pre><code>&lt;?php defined('BASEPATH') OR exit('No direct script access allowed'); ?&gt;
  &lt;?php
  if (isset($_SESSION['logged_in']) != TRUE) {
  echo redirect('/login');
  } 
  ?&gt;
  </code></pre>
  
     
  
  <pre><code>&lt;div class="col-md-9"&gt;
  &lt;div class="list-group"&gt;
  &lt;div class="list-group-item  ucp-nav-bar"&gt;User Control Panel&lt;/div&gt;
  &lt;div class="list-group-item"&gt; 
  </code></pre>
  
   
  <br>
  <br>
  
                  
                      
                  
  
          
          
              
                  
                      
                  
              
          
  
  <pre><code>            &lt;?= form_open() ?&gt;
  
  
  
              &lt;div class="form-group"&gt;
                  &lt;label for="name"&gt;Old Password&lt;/label&gt;
                  &lt;input type="password" class="form-control" id="old_password" name="old_password" placeholder="Enter your current password"&gt;
  
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="email"&gt;New Password&lt;/label&gt;
                  &lt;input type="password" class="form-control" id="new_password" name="new_password" placeholder="Enter a new password"&gt;
  
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="message"&gt;New Password Confrim&lt;/label&gt;
                  &lt;input type="password" class="form-control" id="new_password_confirm" name="new_password_confirm" placeholder="Re-enter the new password for confirmation"&gt;
  
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;input type="submit" class="btn btn-success" value="Change      Password"&gt;
              &lt;/div&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>The problem I am having is that when I submit the input the old password encrypted with hash_password is not the same as the one in the database. 
  The password in the database was encrypted with hash_password.</p>
  
  <p>this is the function for hash_password in the model</p>