重置密码不保存新密码CakePHP 2.0

时间:2014-10-09 04:51:30

标签: php cakephp forgot-password reset-password

我现在已经让系统完成了整个过程但是由于某种原因,实际更新数据库中密码的代码是不是更新了什么?插入新密码和重新密码后,系统立即重定向到登录页面,即使没有显示错误?有什么想法吗?感谢。

这是我的重置功能

function reset($token=null){
    $this->User->recursive=-1;
    if(!empty($token)){
        $u=$this->User->findBytokenhash($token);
        if($u){
            $this->User->id=$u['User']['id'];
            if($this->request->is('post')){
                $this->User->data=$this->request->data;
                $this->User->data['User']['username']=$u['User']['username'];
                $new_hash=sha1($u['User']['username'].rand(0,100));//created token
                $this->User->data['User']['tokenhash']=$new_hash;
                // print_r($this->request->data);
                //      exit;
                if($this->User->validates(array('fieldList'=>array('password','password_confirm')))){
                    if($this->User->save($this->User->data)){
                        $this->Session->setFlash('Password Has been Updated');
                        $this->redirect(array('controller'=>'users','action'=>'login'));
                    }
                } else{
                    $this->set('errors',$this->User->invalidFields());
                }
            }
        } else {
            $this->Session->setFlash('Token Corrupted,,Please Retry.the reset link work only for once.');
        }
    } else{
        $this->redirect('/');
    }
}

这是我的reset.ctp

<?php echo $this->Form->create('User', array('action' => 'reset')); ?>
<?php
    if(isset($errors)){
        echo '<div class="error">';
        echo "<ul>";
        foreach($errors as $error){
            echo "<li><div class='error-message'>$error</div></li>";
        }
        echo "</ul>";
        echo '</div>';
    }
?>
<form method="post">
    <?php
        echo $this->Form->input('User.password', array('type' => 'password',
                                                       'name' => '$data[User][password]',
                                                       'class' => 'form-control'));
        echo $this->Form->input('User.re_password', array('type' => 'password',
                                                        'name' => 'data[User][re_password]',
                                                        'class' => 'form-control'));
    ?>
    <input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
    <?php echo $this->Form->end();?>
</form>

拜托,帮助我,谢谢

3 个答案:

答案 0 :(得分:0)

用这个替换你的ctp代码

 <?php echo $this->Form->create('User'); ?>
<?php
if(isset($errors)){
    echo '<div class="error">';
    echo "<ul>";
    foreach($errors as $error){
        echo "<li><div class='error-message'>$error</div></li>";
    }
    echo "</ul>";
    echo '</div>';
}
?>
<?php
    echo $this->Form->input('User.password', array('type' => 'password',
                                                   'name' => '$data[User][password]',
                                                   'class' => 'form-control'));
    echo $this->Form->input('User.re_password', array('type' => 'password',
                                                    'name' => 'data[User][re_password]',
                                                    'class' => 'form-control'));
?>
<input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
<?php echo $this->Form->end();?>

希望这会奏效。

答案 1 :(得分:0)

我刚注意到,

你请使用$this->User->save($this->request->data)

使用此

查看
    echo $this->Form->input('User.password', array('type' => 'password',
                                                   'class' => 'form-control'));
    echo $this->Form->input('User.re_password', array('type' => 'password',
                                                    'class' => 'form-control'));

让cakephp构建的name属性与数据库表,字段匹配。我认为你只有一个或两个ctp等,你只是从头开始构建你的项目,对吗?

答案 2 :(得分:0)

试试这个:

public function beforeSave() {
    if (isset($this->data['User']['password'])) { 
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    return true;
}

并在功能重置

if($this->User->save($this->User->data,false))
它对我有用。希望它对你有用!!