在CakePHP 2.1中更改用户的密码
发布时间:2020-12-13 16:16:04 所属栏目:PHP教程 来源:网络整理
导读:我正在构建一个用户可以更改密码的页面.他们必须填写两个字段,两个字段必须匹配才能更改,然后成功时会将其重定向到其个人资料页面. 到目前为止,我已经构建了以下方法: public function changePassword(){ $user = $this-User-find('first',array( 'conditio
我正在构建一个用户可以更改密码的页面.他们必须填写两个字段,两个字段必须匹配才能更改,然后成功时会将其重定向到其个人资料页面.
到目前为止,我已经构建了以下方法: public function changePassword() { $user = $this->User->find('first',array( 'conditions' => array( 'User.id' => $this->Auth->user('id')) )); if ($this->request->is('post') || $this->request->is('put')) { if ($this->User->save($this->request->data)) { $this->User->saveField('password',AuthComponent::password($this->request->data['User']['password2'])); $this->Session->setFlash(__('Your password has been changed!')); $this->redirect(array('controller'=>'profiles','action'=>'view','userName'=>$user['User']['username'])); } else { $this->Session->setFlash(__('Whoops! Something went wrong... try again?')); } } } 这是形式: <?php echo $this->Form->create(); ?> <?php echo $this->Form->input('id',array('type'=>'hidden')); ?> <?php echo $this->Form->input('password1',array('type'=>'text','label'=>array('text'=>'Enter your new password'))); ?> <?php echo $this->Form->input('password2','label'=>array('text'=>'Confirm your new password'))); ?> <button type="submit">Save</button> <?php echo $this->Form->end(); ?> 因此,您可以看到计划是使用password2中的内容并使用安全散列将其保存在数据库密码字段中.但是会发生什么呢?它会创建一个新的用户而不是空白数据……我在这里做错了什么?我如何比较两个密码字段…… 解决方法
最可能的问题是你以某种方式将表单中的id丢失回控制器动作.
没有数据数组中的id,蛋糕假定它必须创建一个条目. 因此,调试您发布的数据并确保它不会丢失或更好地在保存之前手动分配ID. $this->request->data['User']['id'] = $this->Session->read('Auth.User.id'); PS:从您的方法看,您的隐藏“id”字段始终为空,因为$user数据不会从控制器传递下来! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |