我是非常新的编程和php ..我正在寻找使用CakePHP创建一个简单的忘记密码系统,用户将输入他们的用户名/电子邮件,然后收到一个链接的电子邮件,他们可以点击创建一个新的密码。
我用Google搜索但没找到任何东西:/
有没有人可以帮我或者给我一个关于在cakephp中忘记或重置密码的简易教程的链接?
答案 0 :(得分:15)
更新
在 UserModel 创建在usertable中添加resetkey字段。
beforeSave
操作中并在该操作中添加此行
public function beforeSave($options = array()) {
parent::beforeSave($options = array());
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
$this->data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
return true;
}
当用户申请忘记passowrd时,请将此重置密钥发送到他的电子邮箱。 的 SomeController 强> 这是一种忘记行动的暗示
if($this->request->is('post')) {
$this->loadModel('User');
$mail = $this->request->data['User']['mail'];
$data = $this->User->findByMail($mail);
$key = $data['User']['resetkey'];
if(!$data) {
$message = __('No Such E-mail address registerd with us ');
$this->Session->setFlash($message,'flash',array('alert'=>'error'));
} else {
$key = $data['User']['resetkey'];
$id = $data['User']['id'];
$mail = $data['User']['email'];
$email = new CakeEmail('smtp');
$email->to($mail);
$email->from("service@localhost.com");
$email->emailFormat('html');
$email->subject('Password reset instructions from');
$email->viewVars(array('key'=>$key,'id'=>$id,'rand'=> mt_rand()));
$email->template('reset');
if($email->send('reset')) {
$message = __('Please check your email for reset instructions.');
$this->Session->setFlash($message,'flash',array('alert'=>'success'));
} else {
$message = __('Something went wrong with activation mail. Please try later.');
$this->Session->setFlash($message,'flash',array('alert'=>'error'));
}
}
$this->redirect('/');
}
将重置密钥发送给用户,以便当用户更改密码时,Auth
将调用beforSave
保存操作并更新重置密钥。所以没有人可以使用相同的密钥访问休息密码页面
电子邮件模板
App/View/Emails/reset.ctp
<p>Please click on the link below to reset your password.</p>
<a href="http://<?= $_SERVER['HTTP_HOST']; ?>/reset/<?= $key .'BXX'.$rand.'XXB'. $id ?>/">Click here to reset your account password</a>
<hr />
<p>Alternatively, you can also copy paste the below link into your browser:
</p>
<p>http://<?= $_SERVER['HTTP_HOST']; ?>/reset/<?= $key .'BXX'.$rand.'XXB'. $id ?>/</p>
<p>This email was sent by <?= APPNAME ?>.</p>
您可以在 APPNAME
中定义App/Config/bootstrap.php
添加以下代码
define('APPNAME','FooBar');
这是重置操作的暗示
$this->loadModel('User');
$a = func_get_args();
$keyPair = $a[0];
$key = explode('BXX', $keyPair);
$pair = explode('XXB',$key[1]);
$key = $key[0];
$pair = $pair[1];
$password = $this->request->data['User']['password'];
unset($this->request->data['User']['password']);
$uArr = $this->User->findById($pair);
if($uArr['User']['resetkey'] == $key) {
$this->User->read(null, $pair);
$this->User->set('password', $password);
if($this->User->save()) {
$message = __('Your password has been reset');
$this->Session->setFlash($message,'flash',array('alert'=>'success'));
} else {
$message = __('Something has gone wrong. Please try later or <b>sign up again</b>');
$this->Session->setFlash($message,'flash',array('alert'=>'alert')); }
} else {
$message = __('<b>Please check your reset link</b>');
$this->Session->setFlash($message, 'flash', array('alert'=> 'error'));
}
在Routs中
Router::connect('/reset/*',array('controller'=>'Home','action'=>'reset'));
您的忘记密码功能已准备就绪
答案 1 :(得分:-1)
我认为这更容易找到并且有效!!