Implement reset password using CodeIgniter

Steps and classes involved in creating a simple forget password functionality in codeigniter based web application.

Sending new random password to the email address provided by user is easy in code ignitor. 

Here are the steps. 

1. Gahter email address from the user. 

2. Check on the databse if the user exists or not. 

3. If email exits create one random password and email to the user. 

4. Save the random password on the users table of the selected user. 

Create controller method
 	public function forget()
	{
		if (isset($_GET['info'])) {
               $data['info'] = $_GET['info'];
              }
		if (isset($_GET['error'])) {
              $data['error'] = $_GET['error'];
              }
		
		$this->load->view('login-forget',$data);
	}
Forget password View
 <div class="container">
		
		<form class="form-horizontal well" method="post" id="form" action="/index.php/login/doforget">
			<fieldset>
	          <legend>Reset password</legend>
			
				<div class="control-group">
					<label for="email"> Email</label>
					<input class="box" type="text" id="email" name="email" />
				</div>
				<div class="form-actions">
					<input type="submit" class="btn btn-primary" value="Reset" />
				</div>
				<?php if( isset($info)): ?>
					<div class="alert alert-success">
						<?php echo($info) ?>
					</div>
				<?php elseif( isset($error)): ?>
					<div class="alert alert-error">
						<?php echo($error) ?>
					</div>
				<?php endif; ?>
				
			</fieldset>
		</form>
	</div>
Do forget method
 public function doforget()
	{
		$this->load->helper('url');
		$email= $_POST['email'];
		$q = $this->db->query("select * from users where email='" . $email . "'");
        if ($q->num_rows > 0) {
            $r = $q->result();
            $user=$r[0];
			$this->resetpassword($user);
			$info= "Password has been reset and has been sent to email id: ". $email;
			redirect('/index.php/login/forget?info=' . $info, 'refresh');
        }
		$error= "The email id you entered not found on our database ";
		redirect('/index.php/login/forget?error=' . $error, 'refresh');
		
	}
Reset password
 private function resetpassword($user)
	{
		date_default_timezone_set('GMT');
		$this->load->helper('string');
		$password= random_string('alnum', 16);
		$this->db->where('id', $user->id);
		$this->db->update('users',array('password'=>MD5($password)));
		$this->load->library('email');
		$this->email->from('cantreply@youdomain.com', 'Your name');
		$this->email->to($user->email); 	
		$this->email->subject('Password reset');
		$this->email->message('You have requested the new password, Here is you new password:'. $password);	
		$this->email->send();
	}
Configure to send email.

Inside the config folder(under application folder) create one config file called email.config. Codeigniter loads the configuration automatically while sending email. 

Email configuration
 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/*$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
*/
$config['protocol'] = 'smtp';
$config['smtp_host']='ssl://smtp.mailgun.org';
$config['smtp_user']='postmaster@cpatt.mailgun.org';
$config['smtp_pass']='youpassword';
$config['smtp_port']='465';
/* End of file config.php */
/* Location: ./application/config/config.php */