我正在使用CakePHP作为我的一个离子应用程序的后端。
这是 Webservicescontroller.php 中的代码,我用于向用户发送重置电子邮件。效果很好,用户可以接收重设电子邮件。
public function forgot_password()
{
$this->isPostValidate();
$response = array();
if ($this->request->is('post')) {
$keys = array('s', 'email');
$this->validatePostData($keys);
$s = $this->filterData('s');
$email = $this->filterData('email');
if (empty($s)) {
$response["error"] = true;
$response["message"] = 'No session exist';
$this->echoResponse(400, $response);
}
$this->validateEmail($email);
$this->loadModel('User');
$this->User->recursive = -1;
$user = $this->User->findByEmail($email);
if (!empty($user)) {
$passkey = md5(uniqid() . $user['User']['email']);
$url = Router::url(array ('controller' => 'users', 'action' => 'reset', true)) . '/' . $passkey;
$timeout = date("F j, Y, H:i", strtotime('+1 hour'));
if ($this->User->updateAll(
array('User.link_id' => "'" . $passkey . "'",
'User.timeout' => "'" . $timeout . "'"),
array('User.id' => $user['User']['id'])
)) {
if ($this->sendResetEmail($url, $user)) {
$response["error"] = false;
$response["message"] = "We have sent a link on your email address to reset it. It'll valid till one hour.";
$this->echoResponse(200, $response);
}
}
$response["error"] = true;
$response["message"] = 'Something seems to be wrong, please try again.';
$this->echoResponse(400, $response);
} else {
$response["error"] = true;
$response["message"] = 'Email address not found.';
$this->echoResponse(400, $response);
}
}
}
function sendResetEmail($url, $user) {
$fromEmail = $this->getSettingKey('fromEmail');
$email = new CakeEmail();
$email->template('resetpw');
$email->emailFormat('both');
$email->from($fromEmail);
$email->to($user['User']['email'], $user['User']['firstname']." ".$user['User']['lastname']);
$email->subject('Reset your password');
$email->viewVars(['url' => $url, 'username' => $user['User']['username']]);
if ($email->send()) {
return true;
} else {
return false;
}
}
这是 Userscontroller.php 中的代码,我用作重置密码的功能。这给了我内部服务器错误
public function reset($passkey = null) {
if ($passkey) {
$query = $this->User->find('all', ['conditions' => ['User.link_id' => $passkey, 'User.timeout' => time()]]);
$user = $query->first();
if ($user) {
if (!empty($this->request->data)) {
// Clear passkey and timeout
$this->request->data['User.link_id'] = null;
$this->request->data['User.timeout'] = null;
$user = $this->User->patchEntity($user, $this->request->data);
if ($this->User->save($user)) {
$this->Flash->set(__('Your password has been updated.'));
return $this->redirect(array('action' => 'login'));
} else {
$this->Flash->error(__('The password could not be updated. Please, try again.'));
}
}
} else {
$this->Flash->error('Invalid or expired passkey. Please check your email or try again');
$this->redirect(['action' => 'password']);
}
unset($user->password);
$this->set(compact('user'));
} else {
$this->redirect('/');
}
}
现在,如果用户单击电子邮件中的链接,他将被重定向到出现内部服务器错误的网页。任何人都可以帮助我,让我知道我在做什么错
reset.ctp
<style>
title{}
button{
border: 1px solid transparent;
font-size: 25px;
border-radius: 5px;
color: #fff !important;
background-color: #428bca;
border-color: #357ebd;
margin-top: 10px;
}
p{color: red;font-size: 15px;}
.reset-form label{font-size: 15px !important;min-width: 135px;}
</style>
<div class="reset-form">
<?php $this->assign('title', 'Reset Password'); ?>
<div class="users form large-9 medium-8 columns content">
<?php echo $this->Form->create($user) ?>
<fieldset>
<legend><?php echo __('Reset Password') ?></legend>
<?php
echo $this->Form->input('new_password', ['required' => true, 'type' => 'password', 'class' => 'input1', 'autofocus' => true]); ?>
<p class="helper">Passwords must be at least 8 characters</p>
<?php
echo $this->Form->input('confirm_password', ['type' => 'password', 'class' => 'input2', 'required' => true]);
?>
</fieldset>
<?php echo $this->Form->button(__('Submit')); ?>
<?php echo $this->Form->end(); ?>
</div>
代码是用Cakephp v 2.1编写的