错误登录后CakePHP身份验证中断

错误登录后CakePHP身份验证中断,php,cakephp,login,authentication,Php,Cakephp,Login,Authentication,我有一个简单的登录表单,用户名和密码。基本上,当用户在网站上的任何位置时,他可以单击一个链接将他带到登录页面。登录后,他会被重定向到最初的页面 问题是: 假设我在“/posts”页面,我点击了登录,所以现在我在“/users/login”上。 -如果登录正确,或者登录不正确,并且用户名文本框中没有填写任何内容,则一切正常(如果正确,我将重定向回“/posts”,否则我将收到“错误的用户名或密码”消息,我尝试再次正确登录,然后重定向到“/posts” 如果登录不正确,并且用户名文本框中填写了一些

我有一个简单的登录表单,用户名和密码。基本上,当用户在网站上的任何位置时,他可以单击一个链接将他带到登录页面。登录后,他会被重定向到最初的页面

问题是:

假设我在“/posts”页面,我点击了登录,所以现在我在“/users/login”上。 -如果登录正确,或者登录不正确,并且用户名文本框中没有填写任何内容,则一切正常(如果正确,我将重定向回“/posts”,否则我将收到“错误的用户名或密码”消息,我尝试再次正确登录,然后重定向到“/posts”

  • 如果登录不正确,并且用户名文本框中填写了一些文本,登录后我会收到“错误的用户名或密码”消息,这很好,但如果我再次填写表单,无论其是否正确,我都会得到“您无权访问该位置”如果我尝试第三次登录,登录可以正常工作,但我被重定向到“/users/posts”而不是“/posts”(存储在会话中的重定向页面的值仍然是“/posts”)
代码如下:

    function beforeFilter () {
        parent::beforeFilter(); 
        $this->Auth->allowedActions = array( 'confirmation');

        $this->Auth->fields = array(
            'username' => 'usr_username', 
            'password' => 'usr_password'
        );
        $this->Auth->userScope = array('User.usr_confirmed' => 1);

        if($this->action == 'signup') {  
            $this->Auth->authenticate = $this->User;  
        }
    }

function login () {
    if ( $this->Auth->user() ) {
        $this->redirect($this->Session->read('page'));
        exit();
    } 
}

function logout () {
    $this->Session->destroy('username');
    $this->redirect('/');
    exit();
}

听起来您的表单操作正在重定向到限制区域。请检查表单操作属性。

将其放在
app/views/layouts/default.ctp
的底部,以帮助调试会话:

<?php debug($session->read()); ?>

如果不是,那么为什么不考虑填充那个键呢?因为 AuthoCuth[/Cuff]已经检查并对这个键起作用,您可能会获得一些免费的功能。 在任何情况下,在您的案例中,自定义的

登录
方法应该是这样的,但是如果上面的任何建议都有效,那么您可以简单地将
登录
方法全部省略,并使用默认功能:

function login () {
    // log the user in
    if ($this->Auth->login()) {
        // your session redirect
        if ($this->Session->read('page')) {
            $this->redirect($this->Session->read('page'));
        }
        // auth's session redirect
        if ($this->Session->read('Auth.redirect')) {
            $this->redirect($this->Session->read('Auth.redirect'));
        }
        // default redirect
        $this->redirect($this->Auth->loginRedirect);
    }
}

感谢您的快速回复。我的表单操作指向/user/login,这是不受限制的。奇怪的是,如果我打印$this->data['user']的内容,在第二次登录后它是空的(当出现“you not authorized..”消息时)。啊,您实际上是对的。我有$form->create('user',array('action'=>'login')),当我用$form->create(null,array('url'=>'/users/login')替换它时,它起了作用。我认为这两个词的意思是一样的。除非Configure::write('Routing.admin','admin'),否则它们会产生相同的url;不知何故,你把它搞砸了,或者你改变了词形变化。
function login () {
    // log the user in
    if ($this->Auth->login()) {
        // your session redirect
        if ($this->Session->read('page')) {
            $this->redirect($this->Session->read('page'));
        }
        // auth's session redirect
        if ($this->Session->read('Auth.redirect')) {
            $this->redirect($this->Session->read('Auth.redirect'));
        }
        // default redirect
        $this->redirect($this->Auth->loginRedirect);
    }
}