Auth login并不总是将会话数据保存到Cakephp中的所有页面
我的登录方式:Auth login并不总是将会话数据保存到Cakephp中的所有页面,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我的登录方式: public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { return $this->redirect($this->Auth->redirect()); } else { $this->Session->setFlash(
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Please enter a valid username or password', 'errors/flash_error');
}
}
}
由于某些原因,它不会总是在重定向到的页面上显示会话信息。我有一个名为/random
的页面,它被路由到:
Router::connect('/random', array('controller' => 'submissions', 'action' => 'random'));
当我转到/random
时,它不会显示以下内容:
<?= $this->Session->read('Auth.User.username'); ?>
以上适用于流行
,最新
,视图
,但不适用于随机
或类别
知道为什么吗?原来我在default.ctp中使用了
flush()
。这导致了奇怪的会话问题。我建议遇到类似问题的人不要使用此功能。尝试使用
。您还可以尝试打印pr($this->Auth)代码>。并检查用户名是否存在于名为数组的“用户”索引中。我使用的是$this->Session->check('Auth.User.username')
,它适用于除/random之外的所有页面
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->loginRedirect = array(
'controller' => 'users',
'action' => 'login');
$this->Auth->allow(
'popular',
'newest',
'random',
'view',
'category',
);
$this->Auth->flash['element'] = 'errors/flash_error';
$this->Auth->authError = 'Please login or sign up to submit a link.';
}