使用CodeIgniter中生成的代码忘记密码

时间:2016-12-19 05:03:34

标签: php codeigniter

我已生成随机代码,但我无法使用数据库表中的密码更新它。

以下是我忘记的密码控制器代码:

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

1 个答案:

答案 0 :(得分:1)

 <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视图

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

此控制器功能

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

此模型函数用于从数据库表中选择电子邮件

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

此模型功能用于创建randome密码并在已注册的电子邮件中发送电子邮件