Today, We want to share with you forgot password in codeigniter.In this post we will show you forgot password using otp in php, hear for codeigniter login and registration with forgot password we will give you demo and example for implement.In this post, we will learn about PHP Change Password Script Using Mysqli with an example.
forgot password with generated code in codeigniter
Forms.php (Controller)
<?php class Forms extends CI_Controller { public function __construct() { /*call CodeIgniter's default Constructor*/ parent::__construct(); /*load database libray manually*/ $this->load->database(); $this->load->library('session'); /*load Model*/ $this->load->helper('url'); $this->load->model('Hello_model'); } public function forgot_pass() { if($this->input->post('forgot_pass')) { $email=$this->input->post('email'); $que=$this->db->query("select pass,email from member_login where email='$email'"); $row=$que->row(); $member_email=$row->email; if((!strcmp($email, $member_email))){ $pass=$row->pass; /*Mail Code*/ $to = $member_email; $subject = "Password"; $txt = "Your password is $pass ."; $headers = "From: [email protected]" . "\r\n" . "CC: [email protected]"; mail($to,$subject,$txt,$headers); } else{ $data['error']=" Invalid Email ID ! "; } } $this->load->view('forgot_pass',@$data); } } ?>
forgot_pass.php (View)
<!DOCTYPE html> <html> <head> <title>Login Form</title> <link rel="stylesheet" type="text/css" href="css/style.css"> <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'> </head> <body> <div id="main"> <div id="login"> <?php echo @$error; ?> <h2>Forgot Password</h2> <br> <form method="post" action=''> <label>Email ID :</label> <input type="password" name="email" id="name" placeholder="Email ID"/><br /><br /> <input type="submit" value="login" name="forgot_pass"/><br /> </form> </div> </div> </body> </html>
Run the program on your browser with URL:
http://localhost/codeIgniter/index.php/Crud/storememberinfo
Forgot password with generated code in CodeIgniter
<form id="resetPassword" name="resetPassword" method="post" action="<?php echo base_url();?>My_Controller/ForgotPassword" onsubmit ='return validate()'> <table class="table table-bordered table-hover table-striped"> <tbody> <tr> <td>Enter Email: </td> <td> <input type="email" name="email" id="email" style="width:250px" required> </td> <td><input type = "submit" value="submit" class="button"></td> </tr> </tbody> </table></form>
Html view
public function ForgotPassword() { $email = $this->input->post('email'); $findemail = $this->MY_model->ForgotPassword($email); if ($findemail) { $this->MY_model->sendpassword($findemail); } else { echo "<script>alert(' $email not found, please enter correct email id')</script>"; redirect(base_url() . 'MY_controller/index', 'refresh'); } }
This controller function
public function ForgotPassword($email) { $this->db->select('email'); $this->db->from('table'); $this->db->where('email', $email); $query=$this->db->get(); return $query->row_array(); }
This model function
public function sendpassword($data) { $email = $data['email']; $query1=$this->db->query("SELECT * from tablename where email = '".$email."' "); $row=$query1->result_array(); if ($query1->num_rows()>0) { $passwordplain = ""; $passwordplain = rand(999999999,9999999999); $newpass['password'] = md5($passwordplain); $this->db->where('email', $email); $this->db->update('employer_registration', $newpass); $mail_message='Dear '.$row[0]['name'].','. "\r\n"; $mail_message.='Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>'.$passwordplain.'</b>'."\r\n"; $mail_message.='<br>Please Update your password.'; $mail_message.='<br>Thanks & Regards'; $mail_message.='<br>Your company name'; require 'PHPMailerAutoload.php'; require 'class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSendmail(); $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = "hostname"; $subject = 'Testing Email'; $mail->AddAddress($email); $mail->IsMail(); $mail->From = 'admin@***.com'; $mail->FromName = 'admin'; $mail->IsHTML(true); $mail->Subject = $subject; $mail->Body = $mail_message; $mail->Send(); if (!$mail->send()) { echo "<script>alert('msg','Failed to send password, please try again!')</script>"; } else { echo "<script>alert('msg','Password sent to your email!')</script>"; } redirect(base_url().'Jobseeker/index','refresh'); } else { echo "<script>alert('msg','Email not found try again!')</script>"; redirect(base_url().'Jobseeker/index','refresh'); } }
I hope you get an idea about forgot password gmail in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.