我做了一个CodeIgniter忘记密码功能,但它无法正常工作

时间:2017-06-15 11:31:26

标签: php codeigniter codeigniter-3 forgot-password

美好的一天!我在CodeIgniter PHP框架上创建了一个忘记密码的功能,但它并没有真正起作用。当我点击发送按钮时,不会发送电子邮件。

一些数据库信息:

  • db name:kadokado
  • db table:users
  • db email column:email
  • db password column:wachtwoord
  • db id列:user_id

我的控制器(Auth.php):

public function forgot()
    {

          $this->load->model('User_model'); // load user model
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header');
            $this->load->view('forgot');
            $this->load->view('templates/footer');
        }else{
            $email = $this->input->post('email');  
            $clean = $this->security->xss_clean($email);
            $userInfo = $this->User_model->getUserInfoByEmail($clean);

            if(!$userInfo){
                $this->session->set_flashdata('flash_message', 'We hebben dit email adres niet kunnen vinden');
                redirect(site_url().'auth/login');
            }   

            if($userInfo->status != true){ //if status is not approved
                $this->session->set_flashdata('flash_message', 'Your account is not in approved status');
                redirect(site_url().'auth/login');
            }

            //build token 

            $token = $this->User_model->insertToken($userInfo->id);                    
            $qstring = $this->base64url_encode($token);                      
            $url = site_url() . 'auth/reset_password/token/' . $qstring;
            $link = '<a href="' . $url . '">' . $url . '</a>'; 

            $message = '';                     
            $message .= '<strong>A password reset has been requested for this email account</strong><br>';
            $message .= '<strong>Please click:</strong> ' . $link;             
            echo $message; //send this through mail
            exit;

        }

    }

public function reset_password()
    {
        $token = $this->base64url_decode($this->uri->segment(4));         
        $cleanToken = $this->security->xss_clean($token);

        $user_info = $this->User_model->isTokenValid($cleanToken); //either false or array();               

        if(!$user_info){
            $this->session->set_flashdata('flash_message', 'Token is invalid or expired');
            redirect(site_url().'auth/login');
        }            
        $data = array(
            'voornaam'=> $user_info->voornaam, 
            'email'=>$user_info->email,                
            'token'=>base64_encode($token)
        );

        $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[wachtwoord]');              

        if ($this->form_validation->run() == FALSE) {   
            $this->load->view('templates/header');
            $this->load->view('reset_password', $data);
            $this->load->view('templates/footer');
        }else{

            $this->load->library('wachtwoord');                 
            $post = $this->input->post(NULL, TRUE);                
            $cleanPost = $this->security->xss_clean($post);                
            $hashed = $this->password->create_hash($cleanPost['wachtwoord']);                
            $cleanPost['wachtwoord'] = $hashed;
            $cleanPost['user_id'] = $user_info->id;
            unset($cleanPost['passconf']);                
            if(!$this->User_model->updatePassword($cleanPost)){
                $this->session->set_flashdata('flash_message', 'Er is iets foutgegaan');
            }else{
                $this->session->set_flashdata('flash_message', 'Uw wachtwoord is geupdate, u kunt nu inloggen');
            }
            redirect(site_url().'auth/login');                
        }
    }

我的模型(User_model.php):

   <?php


class User_model extends CI_Model {


public function getUserInfoByEmail($email)
    {
        $q = $this->db->get_where('users', array('email' => $email), 1);  
        if($this->db->affected_rows() > 0){
            $row = $q->row();
            return $row;
        }else{
            error_log('no user found getUserInfo('.$email.')');
            return false;
        }
    }

     public function getUserInfo($user_id)
    {
        $q = $this->db->get_where('users', array('user_id' => $user_id), 1);  
        if($this->db->affected_rows() > 0){
            $row = $q->row();
            return $row;
        }else{
            error_log('no user found getUserInfo('.$user_id.')');
            return false;
        }
    }

   public function insertToken($user_id)
    {   
        $token = substr(sha1(rand()), 0, 30); 
        $date = date('Y-m-d');

        $string = array(
                'token'=> $token,
                'user_id'=>$user_id,
                'created'=>$date
            );
        $query = $this->db->insert_string('tokens',$string);
        $this->db->query($query);
        return $token . $user_id;

    }

    public function isTokenValid($token)
    {
       $tkn = substr($token,0,30);
       $uid = substr($token,30);      

        $q = $this->db->get_where('tokens', array(
            'tokens.token' => $tkn, 
            'tokens.user_id' => $uid), 1);                         

        if($this->db->affected_rows() > 0){
            $row = $q->row();             

            $created = $row->created;
            $createdTS = strtotime($created);
            $today = date('Y-m-d'); 
            $todayTS = strtotime($today);

            if($createdTS != $todayTS){
                return false;
            }

            $user_info = $this->getUserInfo($row->user_id);
            return $user_info;

        }else{
            return false;
        }

    }    



}

?>

我的观点(reset_password.php):

    <?php   include_once ('templates/header.php');  ?>
<?php   include_once ('templates/sidebar2.php');  ?>

<div class="col-lg-4 col-lg-offset-4">
    <h2>Reset your password</h2>
    <h5>Hello <span><?php echo $voornaam; ?></span>, Voer uw wachtwoord 2x in aub</h5>     
<?php 
    $fattr = array('class' => 'form-signin');
    echo form_open(site_url().'auth/reset_password/token/'.$token, $fattr); ?>
    <div class="form-group">
      <?php echo form_password(array('name'=>'wachtwoord', 'id'=> 'wachtwoord', 'placeholder'=>'Wachtwoord', 'class'=>'form-control', 'value' => set_value('wachtwoord'))); ?>
      <?php echo form_error('password') ?>
    </div>
    <div class="form-group">
      <?php echo form_password(array('name'=>'passconf', 'id'=> 'passconf', 'placeholder'=>'Confirm Password', 'class'=>'form-control', 'value'=> set_value('passconf'))); ?>
      <?php echo form_error('passconf') ?>
    </div>
    <?php echo form_hidden('user_id', $user_id);?>
    <?php echo form_submit(array('value'=>'Reset Password', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
    <?php echo form_close(); ?>

</div>

<div class="clearfix"></div>
<?php   include_once ('templates/footer.php');  ?>

我希望有人可以帮助我!

0 个答案:

没有答案