Undefined index: token Error


#1

I am using the github repository code here: https://github.com/hunzinker/CakePHP-Auth-Forgot-Password

I have used the following function in my UsersController.php. I get the error Undefined index: token on the line that has a comment before it. What should I change?

/**
* Allow user to reset password if $token is valid.
* @return
*/
function reset_password_token($reset_password_token = null) {
if (empty($this->data)) {
$this->data = $this->User->findByResetPasswordToken($reset_password_token);
if (!empty($this->data[‘User’][‘reset_password_token’]) && !empty($this->data[‘User’][‘token_created_at’]) &&
$this->__validToken($this->data[‘User’][‘token_created_at’])) {
$this->data[‘User’][‘id’] = null;
$_SESSION[‘token’] = $reset_password_token;
} else {
$this->Session->setflash(‘The password reset request has either expired or is invalid.’);
$this->redirect(’/users/login’);
}
} else {

//ERROR ON THE NEXT LINE HERE UNDEFINED INDEX: TOKEN
if ($this->data[‘User’][‘reset_password_token’] != $_SESSION[‘token’]) {
$this->Session->setflash(‘The password reset request has either expired or is invalid.’);
$this->redirect(’/users/login’);
}
$user = $this->User->findByResetPasswordToken($this->data[‘User’][‘reset_password_token’]);
$this->User->id = $user[‘User’][‘id’];
if ($this->User->save($this->data, array(‘validate’ => ‘only’))) {
$this->data[‘User’][‘reset_password_token’] = $this->data[‘User’][‘token_created_at’] = null;
if ($this->User->save($this->data) && $this->__sendPasswordChangedEmail($user[‘User’][‘id’])) {
unset($_SESSION[‘token’]);
$this->Session->setflash(‘Your password was changed successfully. Please login to continue.’);
$this->redirect(’/users/login’);
}
}
}


#2

First thing this code is for CakePHP 1.3 application. I am assuming you are trying to use it in CakePHP 3.x. Secondly, the code is not formatted which makes it hard to read. Please reformat the display of this code and I am sure others in here will try to help you solve this issue.