我正在使用 cakephps Auth 组件登录到我的网站。当我正确输入我的用户名和密码时,它会让我登录。然后当我使用loggedIn()检查我是否已登录时,返回true非常不一致。这是我的 AppController,我在其中将 loggedIn() 设置为稍后使用的变量:
<?php
App::uses('Controller', 'Controller');
App::uses('File', 'Utility');
App::uses('AuthComponent', 'Component');
class AppController extends Controller {
public $components = array(
'Session',
'Auth'=>array(
'loginRedirect'=> array('controller'=>'users', 'action'=>'index'),
'logoutRedirect'=> array('controller'=>'users', 'action'=>'index'),
'authError' =>"You can't access that page",
'authorize'=> array('Controller')
)
);
//determines what logged in users have access to
public function isAuthorized($user){
return true;
}
//determines what non-logged in users have access to
public function beforeFilter(){
$this->Auth->allow('index','view');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user', $this->Auth->user());
}
}
这是我使用'logged_in'的一些代码
<?php if($logged_in): ?> //this only returns true some of the time
Welcome <?php echo $current_user['username']; ?>. <?php echo $this->Html->link('Logout', array('controller'=>'users', 'action'=>'login')); ?>
<?php else: ?>
<?php echo $this->Html->link('Login', array('controller'=>'users', 'action'=>'logout')); ?>
<?php endif; ?>
这是我的登录名():
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){ //this returns true every time
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Your username and/or password is incorrect');
}
}
}
我曾尝试调用 $this->Auth->loggedIn() 而不是使用 $logged_in 但我收到无法找到 Auth Helper 的错误。如果需要更多信息来回答我的问题,请告诉我。