密码重置在codeigniter中

时间:2017-07-24 16:15:48

标签: php codeigniter email-verification

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

控制器:

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));
    }

查看:

<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',                            'xxxx@gmail.com');      // e.g. email@example.com
define('EMAIL_BCC',                             'xxxx@gmail.com');      // e.g. email@example.com
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');

问题更新 我改变了观点以加载我的错误,我得到的是“电子邮件失败,再试一次”。邮件未发送错误。感谢

1 个答案:

答案 0 :(得分:0)

从您的评论中看起来您使用的是本地主机服务器。 Localhost服务器无法向IIRC发送电子邮件。要测试发送电子邮件,您必须拥有一个可以访问现实世界的服务器(并且必须在该服务器上启用该功能)。