简体   繁体   English

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

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

I am trying to make a basic user login authentification in CakePHP(2.1.1) ,and it seems to work because it redirects well to users/login (in the url field) BUT when i'm on this page, Firefox says that: 我正在尝试在CakePHP(2.1.1)中进行基本的用户登录身份验证 ,并且似乎可以正常工作,因为当我在此页面上时,它将很好地重定向到用户/登录(在url字段中),但是Firefox表示:

The page isn't redirecting properly. 页面未正确重定向。 Firefox has detected that the server is redirecting the request for this address in a way that will never complete. Firefox已检测到服务器正在以永远无法完成的方式重定向对该地址的请求。 This problem can sometimes be caused by disabling or refusing to accept cookies." 有时可能是由于禁用或拒绝接受Cookie引起的。”

I encounter the same kind of error with Google Chrome, so I think it's really a problem that comes from the coding of CakePHP side, especially a redirection loop . 我在Google Chrome浏览器中遇到了同样的错误,因此我认为这确实是来自CakePHP端编码的问题,尤其是重定向循环

In app\\Controller\\AppController.php I have put : 在app \\ Controller \\ AppController.php中,我输入了:

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

class AppController extends Controller {

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

In app\\Controller\\UsersController.php: 在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());
}

}
?>

in app\\View\\Users\\login.ctp : 在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');
?>

I really don't know where to put new code or modification to stop this redirection loop... :s 我真的不知道在哪里放置新代码或修改内容来停止此重定向循环...:s

Thks in advance! 提前谢谢!

I see two places in your code where redirects can happen at UserController->login() and UserController->logout(). 我在代码中看到两个可以在UserController-> login()和UserController-> logout()进行重定向的地方。 Make sure you have Firebug installed along with Firephp . 确保已将Firebug和Firephp一起安装。 Include the Firephp core library : 包括Firephp核心

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

then add these lines in login() and logout(): 然后在login()和logout()中添加以下行:

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

Turn on the net panel in Firebug, run your code again and if "Login() has been fired" shows up in the Firebug console output you know that it is the login() function that is doing the redirecting. 在Firebug中打开网络面板,再次运行您的代码,如果Firebug控制台输出中显示“ Login()已被触发”,则您知道进行重定向的是login()函数。 Try having firephp log wherever you want to know if a script is going. 尝试将Firephp日志记录到您想知道脚本是否要运行的任何地方。 You can also have it log variables to the console so you can see if things are getting set properly. 您还可以将其记录到控制台中,以查看变量是否设置正确。

Sorry I couldn't diagnose the root of your problem, but this advice should help you troubleshoot. 抱歉,我无法诊断出问题的根源,但是此建议应可帮助您进行故障排除。

In the AppController, you should set these three values like below, it might be because of the login action redirect to itself (login page), you should set loginRedirect to other page than login page 在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