【发布时间】:2014-05-19 11:59:33
【问题描述】:
按照我创建的文档,我无法在 cakePHP 2.4 中登录用户。它说 成功登录 并且还重定向到目标 URL(users/loggedin 操作),但 $this->Auth->username 和 $this->Auth->password 保持空白,没有创建会话。当尝试使用错误的凭据登录时,它也会被重定向。还告诉我登录成功。我现在一无所知。
AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false),
'loginRedirect' => array('controller' => 'users', 'action' => 'loggedin'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'loggedout'),
'authError' => 'UNABLE TO LOG IN.'
)
);
public function beforeFilter(){
$this->Auth->userModel = 'User';
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->allow('index','display','login','show_reg_form', 'view','signup');
}
}
UsersController.php
我只贴登录功能:
public function login() {
if ($this->request->is('post')) {
if($this->Auth->login(/*$this->request->data*/)){
$this->Session->setFlash(__('Successfully logged in.'));
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
login.ctp
相关部分是:
<div class="container-login users form">
<?php echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end(__('Login',true));?>
</div>
【问题讨论】:
-
__('Login',true)应该是__('Login')。这不再是 cake1.3。 -
您是否将参数传递给 $this->Auth->login?或者你只是在这里评论了他们?
-
我没有通过它们,因为当我上次取消注释该部分时,我可以使用任何东西登录。我阅读了文档,它应该可以在没有指定参数的情况下工作。
-
所以使用你提到的参数,它也会为不存在的用户创建一个会话。
-
我从来没有提到你应该使用参数。实际上,如果您手动登录,您只会使用参数。
标签: php cakephp cakephp-2.4