【问题标题】:How to Make Forgot Password in Codeigniter, Password send in email如何在 Codeigniter 中忘记密码,密码通过电子邮件发送
【发布时间】:2018-08-17 09:40:56
【问题描述】:

你好,伙计,我在 codeigniter 中创建了一个小型应用程序,在这个应用程序中我正在忘记密码模块,我创建了一个函数但不知道为什么它不起作用,我需要随机密码必须通过邮件发送是自动生成的,但是email方法不起作用,所以给我一些建议。

这是我的观点:

<form action="<?php echo base_url() . "welcome/forgotpassword" ?>" method="POST">

            <div class="form-group has-feedback">
                    <input type="email" class="form-control" placeholder="Email" name="user_email" />
                    <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
             </div>
              <div class="row">
              <div class="col-xs-4">
                     <input type="submit"  class="btn btn-primary btn-block btn-flat"  value="Send">

              </div>
             </div>
    </form>

这是我的控制器:

public function forgotpassword(){

    $email = $this->input->post('user_email');
    $findemail = $this->main_model->ForgotPassword($email);
    $this->load->view('forgotpassword');
    if ($findemail) {
        $this->main_model->sendpassword($findemail);
    } else {

          $this->session->set_flashdata('msg', 'Email not found!');

    }
}

这是我的模型:

 public function sendpassword($data) {
    $email = $data['user_email'];
    print_r($data);
    $query1 = $this->db->query("SELECT *  from user_registration where user_email = '" . $email . "'");
    $row = $query1->result_array();


    if ($query1->num_rows() > 0) {

        $passwordplain = "";
        $passwordplain = rand(999999999, 9999999999);
        $newpass['user_password'] = md5($passwordplain);
        $this->db->where('user_email', $email);
        $this->db->update('user_registration', $newpass);
        $mail_message = 'Dear ' . $row[0]['full_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 FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPSecure = "tls";
        $mail->Debugoutput = 'html';
        $mail->Host = "ssl://smtp.googlemail.com";
        $mail->Port = 465;
        $mail->SMTPAuth = true;
        $mail->Username = "xxxxxxxxx@gmail.com";
        $mail->Password = "xxxxxxxx";
        $mail->setFrom('xxxxxxx@gmail.com', 'admin');
        $mail->IsHTML(true);
        $mail->addAddress('user_email', $email);
        $mail->Subject = 'OTP from company';
        $mail->Body = $mail_message;
        $mail->AltBody = $mail_message;

        if (!$mail->send()) {


            $this->session->set_flashdata('msg', 'Failed to send password, please try again!');
        } else {

            echo $this->email->print_debugger();
            $this->session->set_flashdata('msg', 'Password sent to your email!');
        }

    }
}

【问题讨论】:

  • 发送纯文本密码是个坏主意。在服务器上设置密码并发送一个安全链接(即包含哈希,而不是密码的链接)以激活它。不要发送密码本身。
  • 是的,你可以给我一个小例子,或者链接@Synchro
  • 看看一些现有的登录系统,如PHPLogin。这比重新发明轮子要好。

标签: mysql codeigniter email phpmailer


【解决方案1】:

这个函数可能与它有关...你能告诉我们吗?

$findemail = $this->main_model->ForgotPassword($email);

当您使用print_r($data) 时,是否会返回任何内容? 如果不是,$query1 将为 0 或 null,一切都会中断。

// core function
public function sendpassword($data) {
    // include your libary at the top
    require FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
    // email retrieved from the ForgotPassword() method.
    $email = $data['user_email'];
    // get the user_info array row
    $query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'");
    $row = $query1->result_array();
    if ($query1->num_rows() > 0) {
        // assign users name to a variable
        $full_name = $row['full_name'];
        // generate password from a random integer
        $passwordplain = rand(999999999, 9999999999);
        // encrypt password
        $encrypted_pass = $this->pass_gen($passwordplain);
        $newpass['user_password'] = $encrypted_pass;
        // update password in db
        $this->db->where('user_email', $email);
        $this->db->update('user_registration', $newpass);
    // begin email functions
    $result = $this->email_user($full_name, $email, $passwordplain);
    echo $result;
    }
}

// email sending
public function email_user($full_name, $email, $passwordplain) {
    // compose message
    $mail_message = 'Dear ' . $full_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';
    // email config
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPSecure = "tls";
    $mail->Debugoutput = 'html';
    $mail->Host = "ssl://smtp.googlemail.com";
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->Username = "xxxxxxxxx@gmail.com";
    $mail->Password = "xxxxxxxx";
    $mail->setFrom('xxxxxxx@gmail.com', 'admin');
    $mail->IsHTML(true);
    $mail->addAddress('user_email', $email);
    $mail->Subject = 'OTP from company';
    $mail->Body = $mail_message;
    $mail->AltBody = $mail_message;
    // send the mail
    if (!$mail->send()) {
        return $this->email->print_debugger();
        $this->session->set_flashdata('msg', 'Failed to send password, please try again!');
    } else {
        return $this->email->print_debugger();
        $this->session->set_flashdata('msg', 'Password sent to your email!');
    }
}

// Password encryption
public function pass_gen($password) {
    $encrypted_pass = md5($password);
    return $encrypted_pass;
}

【讨论】:

  • UPDATE user_registration SET user_password = 'd41d8cd98f00b204e9800998ecf8427e' 其中user_email 为空
  • 我更新了我的答案。我用我自己的风格重用了你的代码,但我错过了 findemail 函数,这会很有趣。如果更新时电子邮件为空,则意味着该函数没有返回任何内容,甚至没有返回错误。
  • 我同意上面的@Synchro 评论,但您需要先完成这项工作,然后才能发送链接。它的原理是一样的。通过电子邮件在查询字符串中发送一个随机字符串变量,该变量将通过您的应用程序中的方法进行验证,并允许用户自己更改密码。你已经完成了大部分工作。仔细检查你的代码
【解决方案2】:

$query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'"); 它有sql注入!

【讨论】:

  • 我认为这不是答案,但您可以将其记为评论
  • 这不是一个答案,因为它缺乏上下文。如果你想解释为什么它有sql注入(也许是一个解释什么是sql注入的链接),它可以被认为是一个答案。
猜你喜欢
  • 2016-08-31
  • 2014-08-11
  • 2011-02-19
  • 2015-06-22
  • 2013-06-13
  • 2013-03-04
  • 2015-10-21
  • 2012-02-29
  • 1970-01-01
相关资源
最近更新 更多