繁体   English   中英

CakePHP 2.1.1>具有基本身份验证的重定向错误(用户/登录)

[英]CakePHP 2.1.1 > Redirection error with Basic Authentification (users/login)

我正在尝试在CakePHP(2.1.1)中进行基本的用户登录身份验证 ,并且似乎可以正常工作,因为当我在此页面上时,它将很好地重定向到用户/登录(在url字段中),但是Firefox表示:

页面未正确重定向。 Firefox已检测到服务器正在以永远无法完成的方式重定向对该地址的请求。 有时可能是由于禁用或拒绝接受Cookie引起的。”

我在Google Chrome浏览器中遇到了同样的错误,因此我认为这确实是来自CakePHP端编码的问题,尤其是重定向循环

在app \\ Controller \\ AppController.php中,我输入了:

<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {

var $components = array('Auth');
}
?>

在app \\ Controller \\ UsersController.php中:

<?php
class UsersController extends AppController {

    public $components = array(
    'Session',
    'Auth' => array(
    'authenticate' => array('Basic')
    )
);

public function login() {
    if ($this->Auth->login()) {
        return $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash('Not able to login');
    }
}

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

}
?>

在app \\ View \\ Users \\ login.ctp中:

<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('login', array('label' => 'Login : '));
echo $this->Form->input('pass', array('type' => 'password', 'label' => 'Password : '));
echo $this->Form->end('Connexion');
?>

我真的不知道在哪里放置新代码或修改内容来停止此重定向循环...:s

提前谢谢!

我在代码中看到两个可以在UserController-> login()和UserController-> logout()进行重定向的地方。 确保已将Firebug和Firephp一起安装。 包括Firephp核心

require_once('FirePHPCore/FirePHP.class.php');
$firephp = FirePHP::getInstance(true);

然后在login()和logout()中添加以下行:

$firephp->log('Login() has been fired.');
$firephp->log('Logout() has been fired.');

在Firebug中打开网络面板,再次运行您的代码,如果Firebug控制台输出中显示“ Login()已被触发”,则您知道进行重定向的是login()函数。 尝试将Firephp日志记录到您想知道脚本是否要运行的任何地方。 您还可以将其记录到控制台中,以查看变量是否设置正确。

抱歉,我无法诊断出问题的根源,但是此建议应可帮助您进行故障排除。

在AppController中,您应该像下面那样设置这三个值,这可能是由于登录操作重定向到其自身(登录页面),所以应该将loginRedirect设置为登录页面以外的其他页面

public function beforeFilter() {
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'home');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM