| 0 | kirasiris | Categories: Tags: , , , ,

CodeIgniter

Let me make it clear, I’m assuming you already know what CodeIgniter is and have lot of previous experience on it. Otherwise, I need you to learn it. From now on, I’m thinking about just pasting code and it will be up to your experience if you get to properly read it.

public function recover(){
  
  // Set rules
  $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
  $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[7]|valid_email');
  
  if($this->form_validation->run() == FALSE){
    
    // Meta data
    $data['title']  = 'Recover Password';

    // Load view
    $this->load->view('users/recover', $data);
  
  } else {
    
    // Verify email exists
    $username   =   $this->input->post('username');
    $email      =   $this->input->post('email');
    $result     =   $this->PublicSession_model->recover($username, $email);
    
    if(!$result){
      
      // Set message
      $this->session->set_flashdata('error', 'Username and E-mail should be attached to a single user');
      // Redirect
      redirect('accounts/recover');
    
    } else {
      
      // Update data - Disable account because user forgot it.
      $uniqueID   =   uniqid('token_'.$username.'_', TRUE);
      $username   =   $this->input->post('username');
      $email      =   $this->input->post('email');
      $userData   =   array(
        'token'     =>  $uniqueID,
        'password'  =>  '',
        'password2' =>  '',
        'status'    =>  'inactive',
      );
      
      $this->PublicSession_model->updateRecover($username, $email, $userData);

      // Get Last ID Data
      $postID = $this->db->insert_id();
      $activityTitle = $username;

      // Send data
      $result =   $this->PublicSession_model->getRecoverEmail($username, $email);

      // Send a link to his/her email to recover it.
      $username   =   $result['username'];
      $this->email->from($this->settings->author_email, $this->settings->author);
      $this->email->to($email);
      $this->email->subject('Recover your password at'.$this->settings->title);
      $this->email->message('Create your password by clicking this link '.$this->settings->site_url.'accounts/verification/'.$uniqueID.'/'.$username);
      $this->email->send();

      // Set message
      $this->session->set_flashdata('success', 'A link to create a new password has been sent to your e-mail');

      // Redirect
      redirect('accounts/recover');
    }
  }
}

public function verification(){
  
  $seg1 = $this->uri->segment(3, 0);
  $seg2 = $this->uri->segment(4, 0);
  
  // Verify token
  $verify = $this->PublicSession_model->getRecoveryToken($seg1, $seg2);
  
  if(!$verify || $seg1 == null || $seg2 == null){
    
    // Set message
    $this->session->set_flashdata('error', "Sorry but ".$seg2." does not seem to have forgotten his/her password. Please, if you're the owner of this account, send us a message to ".$this->settings->author_email." ");
    redirect('accounts/login');
  }
  
  $data['uniqueID'] = $seg1;
  $data['username'] = $seg2;
  
  // Meta data
  $data['title']  = 'Create your password '.$seg2;
  
  // Load view
  $this->load->view('users/verication', $data);

}

public function createnewpassword(){
        
  // Set rules
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|matches[password2]');
  $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|min_length[8]|matches[password]');

  if($this->form_validation->run() == FALSE){
                  
    // Set message
    $this->session->set_flashdata('error', 'Both passwords must match');

    // Load view
    $this->load->view('users/verication');

  } else {

    // Update data
    $username   =   $this->input->post('username');
    $password   =   strip_tags(password_hash($this->input->post('password'), PASSWORD_DEFAULT));

    $newUserPassword = array(
        'token'     =>  NULL,
        'password'  =>  $password,
        'password2' =>  $password,
        'status'    =>  'active',
    );

    $this->PublicSession_model->updateUserPassword($username, $newUserPassword);

    // Set message
    $this->session->set_flashdata('success', "You can now access to your account");

    // Redirect
    redirect('accounts/login');

  }
}

Here are the most important methods in the model for this system:

public function recover($username, $email){
  $this->db->select('*');
  $this->db->where('username', $username);
  $this->db->where('email', $email);
  $query = $this->db->get($this->users);
  return $query->row_array();
}

public function updateRecover($username, $email, $userData){
  $this->db->select('*');
  $this->db->where('username', $username);
  $this->db->where('email', $email);
  $this->db->update($this->users, $userData);
}

public function getRecoverEmail($username, $email){
  $query = $this->db->get_where($this->users, array(
    'username'  =>  $username,
    'email'     =>  $email,
  ));
  return $query->row_array();
}

public function getRecoveryToken($seg1, $seg2){
  $query = $this->db->get_where($this->users, array(
    'token'     =>  $seg1,
    'username'  =>  $seg2,
  ));
  return $query->row_array();
}

public function updateUserPassword($username, $newUserData){
  $this->db->select('*');
  $this->db->where('username', $username);
  $this->db->update($this->users, $newUserData);
}

That is it, hopefully you will be able to copy/paste and put it to work with your system. I know left you saying stuff like “Well, if this guys does not want to explain, then why did he post this?” If you write a comment or send me a message, I will gladly help you.

Leave a Reply

Back to Top