CakePhp 3保持auth会话

时间:2017-05-07 09:34:19

标签: php session authentication cakephp-3.0

关闭浏览器后,我正在尝试在cakephp 3中自动记录用户。我遵循这个post但它没有改变任何东西。 存在具有“my-app”名称的会话,但用户不会自动登录。

我在AppController中的代码:

$this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Usagers',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Admin',
            'action' => 'index'
        ],
        'storage' => [
            'className' => 'Session',
            'key' => 'Auth.User',
        ],
    ]);

我在userController中的代码:

public function login()
{
    $this->viewBuilder()->setLayout('ajax');
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

我不知道为什么我的会话没有保持。

你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

使用安全性和Csrf保护吗?

如果是这样,则需要同时设置两者的到期时间。您可以增加会话时间,但是当默认关闭浏览器时,Csrf cookie将消失。

使用CakePHP 3终止7天的示例

在app.php中:

'Session' => [
    'defaults' => 'php',
    'cookie' => 'CookieName',
    'ini' => [
        'session.cookie_lifetime' => 604800 // 7 days
    ],
    'timeout' => 10080 // 7 days
],

在AppController.php

$this->loadComponent('Csrf',['secure'=>true,'expiry'=> strtotime("+7 day")]);