2017-07-24 101 views
2

我有一个用户系统,用户注册和用户登录。在登录页面上有一个密码重置按钮,在密码休息按钮上有以下代码,但是当我尝试发送密码休息链接时没有任何反应。密码重置在codeigniter

控制器:

function resetPasswordUser() 
    { 
     $status = ''; 

     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('login_email','Email','trim|required|valid_email|xss_clean'); 

     if($this->form_validation->run() == FALSE) 
     { 
      $this->forgotPassword(); 
     } 
     else 
     { 
      $email = $this->input->post('login_email'); 

      if($this->user_model->checkEmailExist($email)) 
      { 
       $encoded_email = urlencode($email); 

       $this->load->helper('string'); 
       $data['email'] = $email; 
       $data['activation_id'] = random_string('alnum',15); 
       $data['createdDtm'] = date('Y-m-d H:i:s'); 
       $data['agent'] = getBrowserAgent(); 
       $data['client_ip'] = $this->input->ip_address(); 

       $save = $this->user_model->resetPasswordUser($data);     

       if($save) 
       { 
        $data1['reset_link'] = base_url() . "resetPasswordConfirmUser/" . $data['activation_id'] . "/" . $encoded_email; 
        $userInfo = $this->user_model->getCustomerInfoByEmail($email); 

        if(!empty($userInfo)){ 
         $data1["username"] = $userInfo[0]->username; 
         $data1["email"] = $userInfo[0]->email; 
         $data1["message"] = "Reset Your Password"; 
        } 

        $sendStatus = resetPasswordEmail($data1); 

        if($sendStatus){ 
         $status = "send"; 
         setFlashData($status, "Reset password link sent successfully, please check mails."); 
        } else { 
         $status = "notsend"; 
         setFlashData($status, "Email has failed, try again."); 
        } 
       } 
       else 
       { 
        $status = 'unable'; 
        setFlashData($status, "It seems an error while sending your details, try again."); 
       } 
      } 
      else 
      { 
       $status = 'invalid'; 
       setFlashData($status, "This email is not registered with us."); 
      } 
      redirect('users/forgotPassword'); 
     } 
    } 

    // This function used to reset the password 
    function resetPasswordConfirmUser($activation_id, $email) 
    { 
     // Get email and activation code from URL values at index 3-4 
     $email = urldecode($email); 

     // Check activation id in database 
     $is_correct = $this->user_model->checkActivationDetails($email, $activation_id); 

     $data['email'] = $email; 
     $data['activation_code'] = $activation_id; 

     if ($is_correct == 1) 
     { 
      $this->load->view('templates/header'); 
      $this->load->view('newPassword', $data); 
      $this->load->view('templates/footer'); 
     } 
     else 
     { 
      redirect('users/login'); 
     } 
    } 

    // This function used to create new password 
    function createPasswordUser() 
    { 
     $status = ''; 
     $message = ''; 
     $email = $this->input->post("email"); 
     $activation_id = $this->input->post("activation_code"); 

     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('password','Password','required|max_length[20]'); 
     $this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]'); 

     if($this->form_validation->run() == FALSE) 
     { 
      $this->resetPasswordConfirmUser($activation_id, urlencode($email)); 
     } 
     else 
     { 
      $password = $this->input->post('password'); 
      $cpassword = $this->input->post('cpassword'); 

      // Check activation id in database 
      $is_correct = $this->user_model->checkActivationDetails($email, $activation_id); 

      if($is_correct == 1) 
      {     
       $this->user_model->createPasswordUser($email, $password); 

       $status = 'success'; 
       $message = 'Password changed successfully'; 
      } 
      else 
      { 
       $status = 'error'; 
       $message = 'Password changed failed'; 
      } 

      setFlashData($status, $message); 

      redirect("users/login"); 
     } 
    } 

MODEL:

function checkEmailExist($email) 
    { 
     $this->db->select('id'); 
     $this->db->where('email', $email); 
     $this->db->where('isDeleted', 0); 
     $query = $this->db->get('users'); 

     if ($query->num_rows() > 0){ 
      return true; 
     } else { 
      return false; 
     } 
    } 


    /** 
    * This function used to insert reset password data 
    * @param {array} $data : This is reset password data 
    * @return {boolean} $result : TRUE/FALSE 
    */ 
    function resetPasswordUser($data) 
    { 
     $result = $this->db->insert('reset_password', $data); 

     if($result) { 
      return TRUE; 
     } else { 
      return FALSE; 
     } 
    } 

    /** 
    * This function is used to get customer information by email-id for forget password email 
    * @param string $email : Email id of customer 
    * @return object $result : Information of customer 
    */ 
    function getCustomerInfoByEmail($email) 
    { 
     $this->db->select('id, email, username'); 
     $this->db->from('users'); 
     $this->db->where('isDeleted', 0); 
     $this->db->where('email', $email); 
     $query = $this->db->get(); 

     return $query->result(); 
    } 

    /** 
    * This function used to check correct activation deatails for forget password. 
    * @param string $email : Email id of user 
    * @param string $activation_id : This is activation string 
    */ 
    function checkActivationDetails($email, $activation_id) 
    { 
     $this->db->select('id'); 
     $this->db->from('reset_password'); 
     $this->db->where('email', $email); 
     $this->db->where('activation_id', $activation_id); 
     $query = $this->db->get(); 
     return $query->num_rows; 
    } 

    // This function used to create new password by reset link 
    function createPasswordUser($email, $password) 
    { 
     $this->db->where('email', $email); 
     $this->db->where('isDeleted', 0); 
     $this->db->update('users', array('password'=>getHashedPassword($password))); 
     $this->db->delete('reset_password', array('email'=>$email)); 
    } 

VIEW:

<div class="row"> 
     <div class="col-md-12"> 
      <?php echo validation_errors('<div class="alert alert-danger alert-dismissable">', ' <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>'); ?> 
     </div> 
    </div> 
    <?php 
    $this->load->helper('form'); 
    $error = $this->session->flashdata('error'); 
    $send = $this->session->flashdata('send'); 
    $notsend = $this->session->flashdata('notsend'); 
    $unable = $this->session->flashdata('unable'); 
    $invalid = $this->session->flashdata('invalid'); 
    if($error) 
    { 
     ?> 
     <div class="alert alert-danger alert-dismissable"> 
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
      <?php echo $this->session->flashdata('error'); ?>      
     </div> 
    <?php } 

    if($send) 
    { 
     ?> 
     <div class="alert alert-success alert-dismissable"> 
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
      <?php echo $send; ?>      
     </div> 
    <?php } 

    if($notsend) 
    { 
     ?> 
     <div class="alert alert-danger alert-dismissable"> 
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
      <?php echo $notsend; ?>      
     </div> 
    <?php } 

    if($unable) 
    { 
     ?> 
     <div class="alert alert-danger alert-dismissable"> 
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
      <?php echo $unable; ?>      
     </div> 
    <?php } 

    if($invalid) 
    { 
     ?> 
     <div class="alert alert-warning alert-dismissable"> 
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
      <?php echo $invalid; ?>      
     </div> 
    <?php } ?> 

    <form action="<?php echo base_url(); ?>users/resetPasswordUser" method="post"> 
     <div class="form-group has-feedback"> 
     <input type="email" class="form-control" placeholder="Email" name="login_email" required /> 
     <span class="glyphicon glyphicon-envelope form-control-feedback"></span> 
     </div> 

     <div class="row"> 
     <div class="col-xs-8"> 
     </div><!-- /.col --> 
     <div class="col-xs-4"> 
      <input type="submit" class="btn btn-primary btn-block btn-flat" value="Submit" /> 
     </div><!-- /.col --> 
     </div> 
    </form> 
    <a href="<?php echo base_url() ?>users/login">Login</a><br> 
    </div><!-- /.login-box-body --> 
</div><!-- /.login-box --> 

常数:

define('EMAIL_FROM',       '[email protected]');  // e.g. [email protected] 
define('EMAIL_BCC',        '[email protected]');  // e.g. [email protected] 
define('FROM_NAME',        'CTL '); // Your system name 
define('EMAIL_PASS',       'Your email password'); // Your email password 
define('PROTOCOL',        'smtp');    // mail, sendmail, smtp 
define('SMTP_HOST',        'smtp.gmail.com');  // your smtp host e.g. smtp.gmail.com 
define('SMTP_PORT',        '25');     // your smtp port e.g. 25, 587 
define('SMTP_USER',        'Your smtp user');  // your smtp user 
define('SMTP_PASS',        'Your smtp password'); // your smtp password 
define('MAIL_PATH',        '/usr/sbin/sendmail'); 

问题UPDATE 我改变了我的看法加载了我的错误,我得到的是“电子邮件已经失败,再试一次。”邮件未发送错误。谢谢

+0

'$ sendStatus = resetPasswordEmail($ data1);'这个定义在哪里? – Kisaragi

+0

您的方法忘记密码() – rowmoin

+0

$ sendStatus = resetPasswordEmail($ data1);在我的帮手 – King

回答

2

更改

define('EMAIL_FROM',       '[email protected]');  // e.g. [email protected] 
define('EMAIL_BCC',        '[email protected]');  // e.g. [email protected] 
define('FROM_NAME',        'xxxx'); // Your system name 
define('EMAIL_PASS',       ''); // Your email password 
define('PROTOCOL',        'sendmail');    // mail, sendmail, smtp 
define('SMTP_HOST',        '');  // your smtp host e.g. smtp.gmail.com 
define('SMTP_PORT',        '');     // your smtp port e.g. 25, 587 
define('SMTP_USER',        '');  // your smtp user 
define('SMTP_PASS',        ''); // your smtp password 
define('MAIL_PATH',        '/usr/sbin/sendmail'); 

,并记住在你的配置包括

 $config['charset'] = 'iso-8859-1'; 
     $config['wordwrap'] = TRUE; 
     $config['mailtype'] = "html"; 
     $config['newline'] = "\r\n"; 

记得标记为正确的,如果你的作品,它应该。

+0

谢谢。有用。谢谢你再次帮助我 – King

0

从您的意见,它看起来像你使用本地主机服务器。本地主机服务器无法将电子邮件从IIRC发送出去。要测试发送电子邮件,您必须拥有可以访问真实世界的服务器(并且必须在该服务器上启用该功能)。

+0

如何配置常量,如果我要使用真实的服务器? – King

+0

这是为XAMPP,但看到这个问题:https://stackoverflow.com/questions/4652566/php-mail-setup-in-xampp。它基本上看起来像你必须在本地服务器上安装一个smtp服务器(或者使用安装了smtp的服务器)。对于常量,您只需根据具体安装更改smtp主机和端口。 – ccopland

+0

在你发送的链接中没有正确的答案 – King