首页 文章

CakePHP 2.0:登录表单不起作用

提问于
浏览
0

我正在尝试创建一个新的注册和登录页面 . 我在登录时遇到问题 .

1)注册用户名和密码后,它成功散列并保存到数据库中,请找到以下代码:(一切都按照蛋糕惯例)

user.php的:

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
......
public function beforeSave() {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}
}
?>

UsersController.php:

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('controller' => 'Posts','action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

public function login() {
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Invalid username or password, try again'), 'default', array(), 'auth');
    }
}

public function logout() {
    $this->redirect($this->Auth->logout());
}

login.ctp:

<fieldset>
<?php echo $this->Session->flash('auth'); ?>
</fieldset>
<?php echo $this->Form->create('Users');?>
<fieldset>
<?php
    echo $this->Form->input('username');
    echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>

调试:(debug($ this-> data);)

来自AppController:

Array
(
[Users] => Array
    (
        [username] => vinodronold
        [password] => vinodronold
    )

)

来自UsersController:

Array
(
[Users] => Array
    (
        [username] => vinodronold
        [password] => vinodronold
    )

)

问题:

每当我访问/ Users / login URL时,我都会收到“无效的用户名或密码,请再试一次”消息 .

此外,我输入正确的用户名和密码后无法登录 .

请帮忙 .

提前致谢!!!

1 回答

  • 0

    仔细观察它,将登录功能的if语句更改为:

    if (!empty($this->request->data['User']) && $this->Auth->login()) {
    

    这将修复访问页面时收到的即时错误消息 .

    我只是注意到你的视图中的表单是错误的 . 模型是单一的 . 将表单创建更改为:

    <?php echo $this->Form->create('User');?>
    

    这应该适用于这两个问题 .

相关问题