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'];
Forget password View
 <div class="container">
		<form class="form-horizontal well" method="post" id="form" action="/index.php/login/doforget">
	          <legend>Reset password</legend>
				<div class="control-group">
					<label for="email"> Email</label>
					<input class="box" type="text" id="email" name="email" />
				<div class="form-actions">
					<input type="submit" class="btn btn-primary" value="Reset" />
				<?php if( isset($info)): ?>
					<div class="alert alert-success">
						<?php echo($info) ?>
				<?php elseif( isset($error)): ?>
					<div class="alert alert-error">
						<?php echo($error) ?>
				<?php endif; ?>
Do forget method
 public function doforget()
		$email= $_POST['email'];
		$q = $this->db->query("select * from users where email='" . $email . "'");
        if ($q->num_rows > 0) {
            $r = $q->result();
			$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)
		$password= random_string('alnum', 16);
		$this->db->where('id', $user->id);
		$this->email->from('', 'Your name');
		$this->email->subject('Password reset');
		$this->email->message('You have requested the new password, Here is you new password:'. $password);	
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';
/* End of file config.php */
/* Location: ./application/config/config.php */