密码重置系统使用Codeigniter

时间:2018-05-03 12:49:54

标签: codeigniter email password-recovery

正在使用密码重置系统,忘记密码的用户可以通过提交注册时使用的电子邮件来请求密码重置链接。我成功创建了电子邮件,它发送了链接,我通过点击它来测试链接。链接经过并加载重置页面,但我的问题是如何让系统识别点击的用户并获取所有详细信息,包括名称,令牌,电子邮件,系统将使用该电子邮件确认用户是请求用户的用户链接。

以下是我到目前为止所做的事情:

控制器

    public function preset(){
        $data['success']='';
        $data['error']='';
        include_once ('query/user_query.php');

        $this->form_validation->set_rules('email','Email','trim|required|valid_email');
         $this->form_validation->set_error_delimiters("<div class='alert alert-warning'><span type='button' class='close' data-dismiss='alert'>&times</span>","</div>");

    if($this->form_validation->run() == false){

        $this->load->view('passwordrecovery.php', $data);
    }
    else{
        $eMail = $this->input->post('email');
         $this->db->where("email = '$eMail'");
        $this->db->from("useraccount");
        $countResult = $this->db->count_all_results();


        if($countResult >=1){
           // $data['firstName'] = '';
          //  $data['lastName'] = '';
            $this->db->where("email = '$eMail'");
            $getUserData =$this->db->get("useraccount")->result();
             foreach($getUserData as $userD){

             $data['firstName'] = $userD->firstname;
             $data['lastName'] = $userD->lastname;
             }
            $sender_email = 'xxx@gmail.com';
            $user_password = 'xxxxxx';
            $token = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 50);
            $subject = 'Password Reset';
            $message = '';
            $message .= "<h2>You are receiving this message in response to your request for password reset</h2>"
                    . "<p>Follow this link to reset your password <a href='".site_url()."/authenticate/resetpassword/.$token' >Reset Password</a> </p>"
                    . "<p>If You did not make this request kindly ignore!</p>"
                    . "<P class='pj'><h2>Kind Regard: Votemate</h2></p>"
                    . "<style>"
                    . ".pj{"
                    . "color:green;"
                    . "}"
                    . "</style>"
                    . "";
            // Configure email library
 $config['protocol'] = 'smtp';
 $config['smtp_host'] = 'ssl://smtp.googlemail.com';
 $config['smtp_port'] = 465;
 $config['smtp_user'] = $sender_email;
 $config['smtp_pass'] = $user_password;
 $config['mailtype'] = 'html';

 // Load email library and passing configured values to email library
 $this->load->library('email', $config);
 //$this->email->set_newline("rn");
 $this->email->set_mailtype("html");

 // Sender email address
 $this->email->from($sender_email);
 // Receiver email address
 $this->email->to($eMail);
// Subject of email
$this->email->subject($subject);
// Message in email
$this->email->message($message);

if ($this->email->send()) {

$eMail = $this->input->post('email');
$ipadd = $this->input->ip_address();
$insert = array(
  'email' => $eMail,
    'ipaddress' => $ipadd,
    'token' => $token
 );

 $this->db->insert('passwordreset', $insert);
 $mail = $this->session->set_userdata('email');
 $data['success'] = 'Email Successfully Send !';
 $this->load->view('linksent.php', $data);
 } else {
 $data['error'] =  '<p class="error_msg">Invalid Gmail Account or Password ! 
  </p>';
 }
 $this->load->view('passwordrecovery.php', $data);
  }


        if($countResult <= 0){

            //user already registered

            $data['error'] = "<div class='alert alert-warning'> Invalid 
    email address<span type='button' class='close' data- 
    dismiss='alert'>&times</span></div>";

            $this->load->view('passwordrecovery.php',$data);

        }

        }


         }

查看

     <div>
                <h1>Password Recovery</h1>
                <h3>Enter your email to receive the password reset link in 
       your Inbox</h3>
                <br/>

                <?php echo form_open('authenticate/preset');?>
                <?php echo $error;?>
                <div class="form-group">
                    <input type="text" name="email" required="required">

                </div>
                <div class="form-group">
                    <input type="submit" value="Send" class="btn-success 
      btn" >
                </div>
                <?php echo form_close()?>
     <br/><br/><br/>


            </div>

数据库:以下是存储信息的数据库:

   CREATE TABLE `passwordreset` (
   `resetid` int(11) NOT NULL,
   `email` varchar(150) NOT NULL,
   `ipaddress` varchar(25) NOT NULL,
   `token` varchar(512) NOT NULL
  ) ENGINE

我需要的帮助是如何获取用户点击其电子邮件中的链接并使用它进行验证并使用它来更新密码的用户的详细信息(姓名,电子邮件,令牌)。感谢

2 个答案:

答案 0 :(得分:3)

当用户点击验证链接并检入控制器方法时,

在网址或隐藏字段中传递用户电子邮件或令牌。

<a href="<?=site_url('user_verification?user_email=' . $user_email . '&user_code=' .  $user_code);?> Click To Verifiy Email </a>

user_verification控制器

public function user_verification_get()
{
   $user_email = $this->input->get('user_email');
   $user_code = $this->input->get('user_code');

   $data=$this->admin_model->user_verification($user_email,$user_code);

   if($data)
    {
        $data['message'] = 'Success.';
    }
    else
    {
        $data['message'] = 'Not Valid User.';
    }
    $this->load->template('verify', $data);
}

模型

public function user_verification($user_email,$user_code){

    $this->db->select('user_email');
    $this->db->where('user_email',$user_email);
    $this->db->where('user_code',$user_code);
    $query = $this->db->get('users');

    if($query->row_array() > 0)
    {
        $data['user_isactive'] = true;

        $this->db->where('user_email',$user_email);
        $this->db->update('users',$data);
        return $query->row_array();
    }
    return false;
}

答案 1 :(得分:0)

您必须创建一个数据库表来存储令牌。在发送电子邮件之前,您必须生成唯一令牌并将其添加到单独的表中。密码重置链接必须包含编码的令牌和用户ID。单击密码重置链接后,您必须检查编码的标记,并且链接中的UserID与数据库中的条目匹配?如果是,则显示更改密码页面,如果没有,则必须显示消息&#34;链接已过期&#34;或者其他什么。

以下是我项目中代码的提示。

array = ['apple', 'banana'];

点击链接后,您必须使用以下代码进行检查:

$act_code = md5(rand(0,1000).'uniquefrasehere');
$activate['UserID']  $USERID;
$activate['TokenNumber'] = $act_code;
$activate['UserEmail'] = $email;
$activate['TokenTime'] = time();
$str_tmp = $this->db->insert_string('forgetpasswordtoken', $activate);
$query_tmp = $this->db->query($str_tmp);

checkforgotpassword函数有什么作用?以下是:

$record = $this->user_model->checkforgot($uid[0], base64_decode($uid[1]));
if($record == true){
    $data['uid'] = $uid[1];
}
else
{
    $msg = "You have already changed your password or your link was expired.!";
}

您可以在链接过期前的几个小时内进一步添加时间限制。 在您的项目中添加此内容后,请告诉我。

谢谢,