title: Codeigniter - Forgot Password
body: <p>Hello: I need to implement a forgot password to a login page. Here I explained what I have so far. </p>
  
  <ol>
  <li>Recover view is prompt to received email input</li>
  <li>Function <code>email_exists()</code> will verify email. If so, <code>send_email()</code> with <code>$temp_pass</code> key and link.The database will store $temp_pass for further action and verification.</li>
  <li>User clicks on the link previously sent passing <code>$temp_pass</code> to function reset_password.</li>
  <li>The model controller will verify <code>$temp_pass</code> with database. If so, load view to input new password - and here is where I am stuck because the form points to a controller that does not recognizes <code>$temp_pass</code> therefore unable to reset password.</li>
  </ol>
  
  <p>How can I retrieved the new password, associated with the right user and reset password?</p>
  
  <p>Code below:</p>
  
  <h2>Controller</h2>
  
  <pre><code>    public function recover(){
      //Loads the view for the recover password process.
      $this-&gt;load-&gt;view('recover');
  }
  public function recover_password(){
      $this-&gt;load-&gt;library('form_validation');
      $this-&gt;form_validation-&gt;set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');
  
              //check if email is in the database
          $this-&gt;load-&gt;model('model_users');
          if($this-&gt;model_users-&gt;email_exists()){
              //$them_pass is the varible to be sent to the user's email 
              $temp_pass = md5(uniqid());
              //send email with #temp_pass as a link
              $this-&gt;load-&gt;library('email', array('mailtype'=&gt;'html'));
              $this-&gt;email-&gt;from('user@yahoo.com', "Site");
              $this-&gt;email-&gt;to($this-&gt;input-&gt;post('email'));
              $this-&gt;email-&gt;subject("Reset your Password");
  
              $message = "&lt;p&gt;This email has been sent as a request to reset our password&lt;/p&gt;";
              $message .= "&lt;p&gt;&lt;a href='".base_url()."main/reset_password/$temp_pass'&gt;Click here &lt;/a&gt;if you want to reset your password,
                          if not, then ignore&lt;/p&gt;";
              $this-&gt;email-&gt;message($message);
  
              if($this-&gt;email-&gt;send()){
                  $this-&gt;load-&gt;model('model_users');
                  if($this-&gt;model_users-&gt;temp_reset_password($temp_pass)){
                      echo "check your email for instructions, thank you";
                  }
              }
              else{
                  echo "email was not sent, please contact your administrator";
              }
  
          }else{
              echo "your email is not in our database";
          }
  }
  public function reset_password($temp_pass){
      $this-&gt;load-&gt;model('model_users');
      if($this-&gt;model_users-&gt;is_temp_pass_valid($temp_pass)){
  
          $this-&gt;load-&gt;view('reset_password');
  
      }else{
          echo "the key is not valid";    
      }
  
  }
  public function update_password(){
      $this-&gt;load-&gt;library('form_validation');
          $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required|trim');
          $this-&gt;form_validation-&gt;set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
              if($this-&gt;form_validation-&gt;run()){
              echo "passwords match"; 
              }else{
              echo "passwords do not match";  
              }
  }
  </code></pre>
  
  <h2>Model_users</h2>
  
  <pre><code>    public function email_exists(){
      $email = $this-&gt;input-&gt;post('email');
      $query = $this-&gt;db-&gt;query("SELECT email, password FROM users WHERE email='$email'");    
      if($row = $query-&gt;row()){
          return TRUE;
      }else{
          return FALSE;
      }
   }
  public function temp_reset_password($temp_pass){
      $data =array(
                  'email' =&gt;$this-&gt;input-&gt;post('email'),
                  'reset_pass'=&gt;$temp_pass);
                  $email = $data['email'];
  
      if($data){
          $this-&gt;db-&gt;where('email', $email);
          $this-&gt;db-&gt;update('users', $data);  
          return TRUE;
      }else{
          return FALSE;
      }
  
  }
  public function is_temp_pass_valid($temp_pass){
      $this-&gt;db-&gt;where('reset_pass', $temp_pass);
      $query = $this-&gt;db-&gt;get('users');
      if($query-&gt;num_rows() == 1){
          return TRUE;
      }
      else return FALSE;
  }
  </code></pre>