忘记密码功能在CodeIgniter中不起作用 [英] Forgot password function not working in CodeIgniter

查看:79
本文介绍了忘记密码功能在CodeIgniter中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!我正在尝试在CodeIgniter框架中创建一个忘记密码的功能,但是当我尝试发送电子邮件时出现2个错误。

Good day! I'm trying to make a forgot password function in the CodeIgniter framework but I'm getting 2 errors when i try to send the e-mail.

某些数据库信息(我正在使用phpMyAdmin):

Some database info (I'm using phpMyAdmin):

Db name: kadokado
Db table name: users
Db email column: email
Db password column: wachtwoord

我的控制器文件(Auth.php):

My controller file (Auth.php) :

    <?php

    class Auth extends CI_Controller{



 public function forgot()
    {

        $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 != $this->status[1]){ //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):

My model file (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):

My view file (reset_password.php) :

 <div class="col-lg-4 col-lg-offset-4">
    <h2>Reset your password</h2>
    <h5>Hello <span><?php echo $firstName; ?></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>

这些是我得到的错误:

    A PHP Error was encountered

Severity: Notice

Message: Undefined property: Auth::$user_model

Filename: controllers/Auth.php

Line Number: 123

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 123
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

第二个错误:

    A PHP Error was encountered

Severity: Error

Message: Call to a member function getUserInfoByEmail() on a non-object

Filename: controllers/Auth.php

Line Number: 123

Backtrace:

我绝对不知道我做错了什么,希望有人能帮助我。
谢谢!

I have absolutely no clue what I'm doing wrong and I hope someone can help me. Thanks!

推荐答案

@frodo再次。

第一个错误::在控制器代码中,您需要首先初始化模型,而不是只能使用model属性。

First Error : in your controller code, you need to initialize model first than only you can use the model property.

public function forgot(){
    // Changes required
    $this->load->model('user_model');
    $userInfo = $this->user_model->getUserInfoByEmail($clean);
}

第二个错误:

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

如何获取 $ this->的值; status [1] 变量。您可以简单地使用 if($ userInfo-> status!= true)

How you get the value of $this->status[1] variable. You can simply use if($userInfo->status != true).

请更改此代码,然后让我知道您是否有任何错误。

Please change this code and let me know if you have any error.

这篇关于忘记密码功能在CodeIgniter中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆