Password Reset in codeigniter

眉间皱痕 提交于 2019-12-23 02:44:08

问题


I have a user system with user registration and user login. on the login page there is a password reset button and on the password rest button the following codes are there but nothing happens when I try to send a password rest link.

CONTROLLER:

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 -->

CONSTANT:

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

QUESTION UPDATE I changed my view to load out my errors and what I get is "Email has failed, try again." Error for mail not sent. Thanks


回答1:


From your comments, it looks like you are using a localhost server. Localhost servers cannot send emails out IIRC. To test sending emails, you have to have a server that has access to the real world (and the feature has to be enabled on that server).



来源:https://stackoverflow.com/questions/45285443/password-reset-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!