0

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

这是我的重置功能

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>

请帮帮我,谢谢

4

3 回答 3

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();?>

希望这会奏效。

于 2014-10-09T05:17:41.670 回答
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 等,您只是从头开始构建项目,对吗?

于 2014-10-09T07:26:44.397 回答
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))

它对我有用。希望这对你有用!!

于 2018-02-16T17:54:36.843 回答