2017-07-24 98 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