Laravel Cookie会话生存期 [英] Laravel cookie session lifetime

查看:144
本文介绍了Laravel Cookie会话生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Laravel用作OAuth2客户端,我需要保留令牌cookie.因此,我将驱动程序设置为cookie并保留默认值,直到生命周期120为止.

当任何用户检查登录时都记得我时,我尝试使用以下代码更改生命周期:

    $lifetime = time() + 60 * 60 * 24 * 365;// one year
    Config::set('session.lifetime', $lifetime);

但是没有成功.在任何其他控制器中,我都会检查生命周期的值,并在每次获得默认值时进行检查.

\Log::info(\Config::get('session.lifetime'));

编辑#1:

足够了吗?

if(Input::has('rememberMe')) {
   $lifetime = time() + 60 * 60 * 24 * 365; // one year
   Session::put('Expires', $lifetime);
}

编辑#2:

我以与上面示例中的Expires相同的方式放置acess_token密钥,例如:

public function signin() {

    /**
     * Code for getting *client_code* and *client_state* from API server
     */

    $access_token = $this->provider->getAccessToken('authorization_code', $form_data);

    // $access_token is object and contain all data (access_token, refresh_token, expires)
    Session::put('access_token', $access_token);
    Session::put('refresh_token', $access_token->refreshToken);
    Session::put('token_expires', $access_token->expires);

    if(Input::has('rememberMe')) {
       $lifetime = time() + 60 * 60 * 24 * 365; // one year
       Session::put('expires', $lifetime);
    }


    return Response....

}

这是默认" Laravel会话(我在/app/config/session.php中将驱动程序从 file 更改为 cookie ).我知道寿命应该在/app/config/session.php文件中设置,但是如您所见,记住我"选项需要更长的寿命

解决方案

实际上,当您在Controller中设置这样的值时:

$lifetime = time() + 60 * 60 * 24 * 365;// one year
Config::set('session.lifetime', $lifetime);

它不是更新文件中的值,而是仅将其设置为当前请求(在内存中),并且当您从另一个控制器/请求中使用此值检查该值时,如下所示:

Config::get('session.lifetime');

您正在从文件系统的原始值中获取值.如下文档所述:

仅在运行时设置配置值 当前请求,并且不会继续到后续请求.

I used my Laravel as a OAuth2 client, and I need to keep token i cookies. So, I set driver to cookie and keep default value for lifetime 120

When any user check remember me on login, I tried to change lifetime with code:

    $lifetime = time() + 60 * 60 * 24 * 365;// one year
    Config::set('session.lifetime', $lifetime);

But without success. In any another controller I checked value of lifetime and every time I get default value.

\Log::info(\Config::get('session.lifetime'));

Edit #1:

It is enough?

if(Input::has('rememberMe')) {
   $lifetime = time() + 60 * 60 * 24 * 365; // one year
   Session::put('Expires', $lifetime);
}

Edit #2:

I put acess_token key on the same way as Expires in example above, like:

public function signin() {

    /**
     * Code for getting *client_code* and *client_state* from API server
     */

    $access_token = $this->provider->getAccessToken('authorization_code', $form_data);

    // $access_token is object and contain all data (access_token, refresh_token, expires)
    Session::put('access_token', $access_token);
    Session::put('refresh_token', $access_token->refreshToken);
    Session::put('token_expires', $access_token->expires);

    if(Input::has('rememberMe')) {
       $lifetime = time() + 60 * 60 * 24 * 365; // one year
       Session::put('expires', $lifetime);
    }


    return Response....

}

This is the 'default' Laravel session (I changed driver from file to cookie in /app/config/session.php). I know life time should be set in /app/config/session.php file, but as you can see I need longer life time for Remember me option

解决方案

Actually when you are setting the value like this in a Controller:

$lifetime = time() + 60 * 60 * 24 * 365;// one year
Config::set('session.lifetime', $lifetime);

It's not updating the value in the file, instead it sets it for the current request only (in memory) and when you check the value using this from another Controller/Request like this:

Config::get('session.lifetime');

You are getting the value from the original value from file system. It's mentioned in the documentation as given below:

Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.

这篇关于Laravel Cookie会话生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆