Hello friends, welcome to part 7 of CodeIgniter 3. Today I am going to learn you Forgot password function. I have already teach you all about register, login and email verification in last few parts. If you have not learned, you have to also learn that.
What is the user can never forget the password. Now he will not register again and again with new email. How can we believe that this is his account. A web developer should never trust a user. We will ask the user for his register email. Brother, tell us your register email, we send you a link and a OTP to create a new password. right?
let’s create a page for the user with the Forgotten password. Let’s create an input box in which we will ask to enter his email. If he enters the correct email which is available in our database, we will send a OTP to him to create a new password. Let us code the input email box. Let’s create a file in the view folder named Forgot php.
Forgot php (view)
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <?php echo form_open('register/forgot'); ?> <!-- on success --> <?php if($error=$this->session->flashdata('otp_failed')): ?> <div class="row"> <div class="col-lg-6"> <div class="alert alert-danger"> <?= $error; ?> </div> </div> </div> <?php endif; ?> <div class="row text-warning"> <div class="col-sm-6 p-0"> <div class="form-horizontal"> <h4>Forgot your password?</h4> <hr /> <div class="form-group"> <Label for="">Enter Your Registered Email ID to ResetPassword</Label> <input type="email" name="Email" placeholder="Email ID" class="form-control" /> <div class="text-danger"><?php echo form_error('Email'); ?></div> </div> <div class="form-group"> <input class="btn btn-danger btn-sm" type="submit" value="Get OTP"> </div> </div> </div> </div>
Do not make copy paste work. You have to write and understand the code. I will speak to you in my lesson only with the theory. You have to practice if you want to learn coding. We carry forward Now we will go to our register controller. First we will run the form validation. If run, we will store the user’s input in the email variable and send it to the model.
Register (Controller)
public function forgot() { if($this->session->userdata('id')){ redirect('/'); } $this->load->library('form_validation'); $this->form_validation->set_rules('Email', 'Email', 'trim|required', array('required' => 'You must provide a %s.')); if ($this->form_validation->run()) { $otp = rand(100000,999999); $Email=$this->input->post('Email'); $this->load->model('registermodel'); if($this->registermodel->if_email_exist($Email,$otp)) { $this->session->set_flashdata('otp_success','OTP successfully sended/Check your mail !!'); return redirect('register/updatepassword'); } else { $this->session->set_flashdata('otp_failed','Email not exist or Email not verified !!'); return redirect('register/forgot'); } } else { $this->load->view('header'); $this->load->view('forgot'); $this->load->view('footer'); } }
Register (model)
Now we have to go into the model, we will check whether the email that the user has entered is in the database or not. If the email is available, we will send that email OTP. OTP to generate new password. And if the email is not a database, then we will display the error message to the user, as we have done in the register controller.
public function if_email_exist($Email,$otp) { $q=$this->db->select() ->from('users') ->where(['Email'=>$Email]) ->get(); $username= $q->row()->UserName; if($q->num_rows()) { $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'smtp.masterarts.in',// change it yours 'smtp_port' => 587,// change it yours 'smtp_user' => 'moderation@masterarts.in', // change it to yours 'smtp_pass' => '********', // change it to yours 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from('moderation@masterarts.in', "masterarts Team"); $this->email->to($Email); $this->email->subject("masterarts Forgot password"); $this->email->message('<h3>Dear User</h3><br>'.'User Name: '.$username.'<br>Your One Time Password: '.$otp.'<br>Click <a href="https://masterarts.in/register/updatepassword">here</a> to set password<br><h3>Thanks & Regards,<br>Moderation Team</h3>'); $this->email->send(); $this->db->set(['email_verification_code'=>$otp]) ->where(['Email'=>$Email]) ->update('users'); return true; } else { return false; } }
friends we have to add new column in database “email_verification_code”. We have to create new columns to keep the OTP saved. To verify later. If you do not know how to add a new column, then you can learn from here. Follow this link https://masterarts.net/explain-how-to-create-database-and-tables-and-how-to-insert-update-and-select-records-in-phpmyadmin-in-wamp-server-step-by-step-with-example/
Now we have to create update new password view and controller function.
update password (view)
We have to create three input boxes in the view.
- OTP
- password
- password confirm
<?php echo form_open('register/updatepassword'); ?> <!-- START: UPDATE FORM --> <!-- on success --> <?php if($error=$this->session->flashdata('otp_success')): ?> <div class="row"> <div class="col-lg-6"> <div class="alert alert-success"> <?= $error; ?> </div> </div> </div> <?php endif; ?> <!-- on success --> <?php if($error=$this->session->flashdata('update_failed')): ?> <div class="row"> <div class="col-lg-6"> <div class="alert alert-danger"> <?= $error; ?> </div> </div> </div> <?php endif; ?> <script type="text/javascript"> function myFunction() { var x = document.getElementById("myInput"); if (x.type === "password") { x.type = "text"; } else { x.type = "password"; } } </script> <div class="row"> <div class="col-sm-6 p-1 text-warning"> <p style="font-size: larger;">Update Password</p> <hr/> <lable>OTP</lable> <input class="form-control" type="text" name="otp" value="<?php echo set_value('otp'); ?>" placeholder="Enter OTP" size="50" /> <div class="text-danger"><?php echo form_error('otp'); ?></div> <lable>New Password</lable> <input class="form-control" type="password" id="myInput" name="Password" value="<?php echo set_value('Password'); ?>" placeholder="Enter Password" size="50" /> <div class="text-danger"><?php echo form_error('Password') ?></div> <lable>Password Confirm</lable> <input class="form-control" type="password" name="passconf" value="<?php echo set_value('passconf'); ?>" placeholder="Re-Enter Password" size="50" /> <div class="text-danger"><?php echo form_error('passconf'); ?></div> <input type="checkbox" onclick="myFunction()"> Show Password <div class="col-md-offset-2 col-md-10 p-2"> <input class="btn btn-danger btn-sm" type="submit" value="Update" /> <input class="btn btn-warning btn-sm" type="reset" value="Reset" /> </div> </div> </div> <!-- END: UPDATE FORM -->
update password (Register controller)
public function updatepassword() { if($this->session->userdata('id')){ redirect('/'); } $this->load->library('form_validation'); $this->form_validation->set_rules('otp', 'otp', 'trim|required|min_length[3]|max_length[6]|numeric', array('required' => 'You must provide a %s.')); $this->form_validation->set_rules('Password', 'Password', 'trim|required|min_length[5]|max_length[20]'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[Password]'); if ($this->form_validation->run()) { $otp=$this->input->post('otp'); $Password=$this->input->post('Password'); $pwd_hash=password_hash($Password,PASSWORD_BCRYPT); $this->load->model('registermodel'); if($this->registermodel->update_password($otp,$pwd_hash)) { $this->session->set_flashdata('update_success','successfully Updated/ Login to your Ac!!'); return redirect('login'); } else { $this->session->set_flashdata('update_failed','Otp not match or Otp expired!!'); return redirect('register/updatepassword'); } } $this->load->view('header'); $this->load->view('updatepassword'); $this->load->view('footer'); }
update password (Register model)
public function update_password($otp,$pwd_hash) { $q=$this->db->where('email_verification_code',$otp) ->get('users'); if($q->num_rows()) { $this->db->set('Password',$pwd_hash) ->where('email_verification_code',$otp) ->update('users'); return true; } else { return false; } }
hope you understand everything properly. if you have any doubt you can comment us. i will defiantly help you.
happy coding