令牌生成忘记密码

时间:2016-09-03 08:40:30

标签: codeigniter

我创建了忘记密码模块。 我的问题:如何使用codeigniter生成忘记密码的令牌。我使用了codeignitor框架。下面的代码

控制器:

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('user_model');
        $this->load->library('session');
    }

 public function forgotpassword() {
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_emails');
        if ($this->form_validation->run() == FALSE) {
            $data = array(
                'page_title' => 'Forgot Password',
                'page_name' => 'user/forgotpassword'
            );
            $this->load->view('user_template', $data);
        } else {
            $result = $this->user_model->forgotpassword($_POST);
            if (!empty($result)) {
                echo '<h1 class="text-center">Thank You</h1>';
            }
        }
    }
}

型号:

class User_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

 public function forgotpassword($data) {
        return $this->db->get_where('user', array('email' => $data['email']))->row_array();
    }
}

查看:

<section class="container">
    <section class="login-form">
        <?php echo form_open('User/forgotpassword'); ?>
        <section>
            <h2><span style="color: red">For</span>The<span style="color: red">Love</span>Of<span style="color: red">Food</span>Trucks</h2>
        </section>

        <div class="text-danger">
            <?php echo validation_errors('<li>', '</li>'); ?>
        </div>
        <input type="hidden" name="token" value=" <?php echo sha1(date("Y/m/d h:i:sa")); ?>">
        <label>Email</label>
        <input type="text" name="email" id="email" class="form-control" placeholder="Email Address"  value="<?php echo set_value('email'); ?>"required=""/>
        <br/>
        <button type="submit" name="submit" class="btn btn-block btn-danger">Submit</button>
        <?php echo form_close(); ?>
    </section>
</section>

请帮帮我............................................ .................................................. ....

1 个答案:

答案 0 :(得分:0)

使用PHP生成令牌的方法有很多,其中之一是:

$forgotten_code = sha1(uniqid(rand()));

请注意,这只是关于&#39; 如何生成代码&#39;的问题的答案。但是涉及恢复密码的过程比你想象的要复杂得多。

我可以建议你使用ion auth 2之类编写的Codeigniter类,你可以在这里找到:

https://github.com/benedmunds/CodeIgniter-Ion-Auth

此课程将涵盖所有用户身份验证,登录,密码恢复等等:

您无需重新发明

顺便说一下,你有一个拼写错误:

'Email Address', 'required|valid_emails'

你需要像这样纠正:

'Email Address', 'required|valid_email'