Tejaswee
Tejaswee

Reputation: 159

Forgot password with generated code in CodeIgniter

I have generated random code but I cannot update it with password from database table.

Below is my forgot password controller code:

public function forgot(){
    if($this->input->post('submit')) {
        $email = $this->input->post('email');
        $qry['email'] = $this->Employer_model->select($email);
        $email1 = $qry['email']->email;

        if($email1 == $email) {
            $code1 = rand();
            $code = md5($code1);
            $employer_id = $GLOBALS['employer_id'];
            $qry1 = $this->Employer_model->insertpwd($code, $employer_id);

            if($qry1) {
                echo "<script>alert(' your new password is $code1!')</script>";
            }
        }
    }

    $this->load->view('employer/forgot');
}

Upvotes: 0

Views: 14866

Answers (1)

Tejaswee
Tejaswee

Reputation: 159

 <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 for select email from database table

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');
    }
}

This model function for create randome password and send email on registered email

Upvotes: 3

Related Questions