[英]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.