Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Auth login并不总是将会话数据保存到Cakephp中的所有页面_Php_Cakephp_Cakephp 2.0 - Fatal编程技术网

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.';
}