CakePHP 2.4忘记密码



我刚刚开始使用CakePHP,并且喜欢使用它!我已经创建了一个登录系统和注册系统,但我真的很难处理"忘记密码"部分。

我想在用户数据库中使用tokenhash和到期日,这样它就不会被滥用,用户需要输入用户名和电子邮件,才能通过新生成的tokenhash获得激活链接

有很多教程,但我发现其中大多数适用于第一部分,例如通过电子邮件发送激活链接/重置令牌和计时器,但在更改密码时似乎都失败了。

请帮助我,或者从网上获得工作教程,或者应用上述必需内容的解决方案。

提前感谢Steve

下面我正在为我的一个项目编写代码,这可能会对您有所帮助。

1-我创建了一个新表,其中包含每个用户的唯一令牌。

表名:-user_password_resets

列:userclient_id,令牌

2-电子邮件模板名称为:-webroot/template/change_password.html 内的-change_password.html

public function login_send() {
       $this->isLoggedIn(); //Check if the user is logged in
      if($this->request->is('post')) { #if the form is submitted
        $login = $this->data['User']['login'];
        $conditions = array('User.login'=>$login);
        if($this->User->hasAny($conditions)) {
            $users = $this->User->find('first', array('conditions'=>$conditions));
            #Generate the token
            $token = md5(uniqid(rand(),true));
            #Save token and other details in user_password_reset_links table
            $users = $this->User->find('first', array('conditions'=>array('User.login'=>$login)));
            $my_name = $users['User']['first_name'];
            $reset_links = array();
            $reset_links['UserPasswordReset']['userclient_id'] = $users['User']['client_id'];
            $reset_links['UserPasswordReset']['token'] = $token;
            $conditions = array('UserPasswordReset.userclient_id'=>$users['User']['client_id']);
            if($this->UserPasswordReset->hasAny($conditions)) {
                 $user_id = $users['User']['client_id'];
                $this->UserPasswordReset->updateAll(array('UserPasswordReset.token'=>"'$token'"), array("UserPasswordReset.userclient_id"=>"$user_id"));    
            } else {
                $this->UserPasswordReset->create();
               $this->UserPasswordReset->save($reset_links);
            }
            $password_reset_link = BASE_URL."users/reset_password/$token";
            #Send Welcome Email
            $mailContent = file_get_contents(BASE_URL . "templates/change_password.html");
            $rootlink = BASE_URL;
            $arrMail = array(
                "{NICK}" => ucfirst($my_name),
                "{rootlink}" => BASE_URL,
                "{SITE_TITLE}" => SITE_TITLE,
                "{PASSWORD_RESET_LINK}"=>$password_reset_link
                );
             $mails = explode(',', $users['User']['email']);    
            $msg = @str_replace(array_keys($arrMail), array_values($arrMail), $mailContent);
            $data = array();
            $data['to'] = @$mails[0];
            $data['body'] = $msg;
            $data['subject'] = SITE_TITLE.'- Reset Password.';
            $this->send_mail($data);
            $this->Session->setFlash('A password reset link has been sent to the email address.', 'default', array('class'=>'successMsg'));
            $this->redirect(array('controller'=>'users', 'action'=>'login'));
            exit;
        } else {
            $this->Session->setFlash('The Username entered is not registered with Captain Marketing.', 'default', array('class'=>'errorMsg'));
            $this->redirect(array('controller'=>'users', 'action'=>'login_send'));
            exit;
        }
    }
    $this->set('title_for_layout', '-Send password reset link');
  }

最新更新