会话过期即使我的工作,会话生存期,Ajax和Symfony2的 [英] Session expires even if I'm working, session lifetime, Ajax and Symfony2

查看:311
本文介绍了会话过期即使我的工作,会话生存期,Ajax和Symfony2的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经配置在超时我申请关闭会话,如果用户做在10分钟时间罢了。在 config.yml 我有这样的:

I have configured my application for close session on timeout if user do nothing during 10 minutes period. At config.yml I have this:

session:
    handler_id:  ~
    cookie_lifetime: 600 # 10 minutes
    gc_maxlifetime: 600 # 10 minutes
    gc_probability: 1
    gc_divisor: 1

我做一个AJAX调用每隔一分钟检查,如果会议将接近过期或没有,这就是我检查:

I do a Ajax call every one minute to check if session will be close to expire or doesn't and this is what I check:

public function isLoggedInAction(Request $request)
{
    $response = array();
    $response['authenticated'] = FALSE;
    $status = 200;
    $securityContext = $this->container->get('security.context');
    if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
        $response['authenticated'] = TRUE;
    }

    return new JsonResponse($response, $status ?: 200);
}

对于一些的未知的原因不能正常使用,并每隔10分钟会被关闭是否我正与网页或没有,为什么?我失去了一些东西?

For some unknow reason is not working and every 10 minutes session is closed whether I'm working with page or doesn't, why? I'm missing something?

修改1 试过了新的价值,仍然没有工作:

Edit 1 Tried new values, still not working:

session:
    handler_id:  ~
    cookie_lifetime: 1800
    gc_maxlifetime: 600
    gc_probability: 1
    gc_divisor: 100

虽然我是工作在页面上,执行Ajax调用和会话关闭等等一些其他的任务,是行不通的。这一点的唯一值的显然的工作对我直到现在设置 cookie_lifetime:86400#1天这是疯了,我

While I was working on the page, performing Ajax calls and some other tasks the session was closed so is not working. The only value that apparently works for me til now is set cookie_lifetime: 86400 #1 day which is crazy to me!

编辑2 之后的 @acontell 建议为解决虚拟机的时间和日期我试图用这个新的值(10分钟的时间太长,所以我已经改变3):

Edit 2 After @acontell suggestion for fix the VM time and date I'm trying with this new values (10 minutes takes too long so I've changed to 3):

session:
    handler_id:  ~
    cookie_lifetime: 1800
    gc_maxlifetime: 180 # session will expire after 3 minutes of inactivity
    gc_probability: 1
    gc_divisor: 100

和我也固定在虚拟机上的日期/时间通过启用 NTPD 服务,现在日期是蛮好的:

And also I fixed the date/time on the VM by enabling ntpd service and now date is just fine:

[root@webvm var]# date
 Sun Feb  1 18:35:17 VET 2015

但5分钟后(函数调用被执行5次),会议还活着。这就是我如何调用该函数 isLoggedInAction()从JavaScript端:

But after 5 minutes (function call was executed 5 times) session still alive. This is how I call the function isLoggedInAction() from Javascript side:

$.post(Routing.generate('isLoggedIn',{}),{},'json').done(function (data, textStatus, jqXHR){
    if( data.authenticated ){
        var timer = window.setInterval(function(){
            $.post(Routing.generate('isLoggedIn',{}),{},'json').done(function (data, textStatus, jqXHR){
                if( !data.authenticated ){
                    window.clearInterval(timer);
                    $.growl({
                        message: 'La sesión ha expirado por inactividad, debe <a href=""><b>iniciar seción</b></a> nuevamente.'
                    }, {
                        type: "danger",
                        allow_dismiss: false,
                        timer: 10000,
                        animate: {
                            enter: 'animated fadeInDown',
                            exit: 'animated fadeOutUp'
                        },
                        onHide: function(){
                            location.reload();
                        }
                    });
                }
            }).fail(function(){});
        },60000);
    }
}).fail(function(){});

请参阅下图:

测试3

在说一切都工作正常,我做了最新的和明确的测试:打开应用程序,并在所有的夜晚(约8小时)离开不变和惊喜它永远不会关闭会话。正如下面显示的图像看有多少请求的页面不,看看会议还活着,为什么?

After say all was working fine I did the latest and definitive test: open the application and leave untouched during all the night (almost 8 hours) and surprise it never closes the session. As image below show see how many request the page does and see how session still alive, why?

Ajax调用由每10.5分

Ajax call is made every: 10.5 minutes

$.post(Routing.generate('isLoggedIn',{}),{},'json').done(function (data, textStatus, jqXHR){
    if( data.authenticated ){
        var timer = window.setInterval(function(){
            $.post(Routing.generate('isLoggedIn',{}),{},'json').done(function (data, textStatus, jqXHR){
                if( !data.authenticated ){
                    window.clearInterval(timer);
                    $.growl({
                        message: 'La sesión ha expirado por inactividad, debe <a href=""><b>iniciar seción</b></a> nuevamente.'
                    }, {
                        type: "danger",
                        allow_dismiss: false,
                        timer: 10000,
                        animate: {
                            enter: 'animated fadeInDown',
                            exit: 'animated fadeOutUp'
                        },
                        onHide: function(){
                            location.reload();
                        }
                    });
                }
            }).fail(function(){});
        }, 210000);
    }
}).fail(function(){});

设置说,会议应当通过到期:10分

Settings say that session should expire passed: 10 minutes.

session:
    handler_id:  ~
    cookie_lifetime: 630000
    gc_maxlifetime: 630000 # session will expire after 10 minutes of inactivity 
    gc_probability: 1
    gc_divisor: 100

在服务器时间是罚款:

Time at server is fine:

[root@webvm sencamer.dev]# date
Mon Feb  2 07:26:53 VET 2015

我还需要检查?

What else should I check?

测试5

好吧,我还在做测试,因为这已经不是一个很好的行为。所以,这是我一直做这个测试:

Ok, I'm still doing test because this has not a good behavior. So, this is what I've do for this test:

  • 打开应用程序,并开始工作就可以了
  • 在某一时刻停止工作并离开该应用程序发出的Ajax调用来检查是否会话还活着与否。 (会话仍然活着见下图)
  • 在我继续工作的应用程序的图像2 的节目,但惊喜会话结束,应用靠拢,首先调用之后。
  • Open the application and start working on it
  • At some moment stop working and leave the the application made the Ajax call to check whether session still alive or not. (session still alive see image below)
  • After that first call I continue working on the application as image 2 shows but surprise session ends and the application gets close.

为什么呢?是什么造成了这种行为?是这样吗基于我的参数?

Why? What is causing that behavior? Is that right based on my parameters?

此图像显示了第一个也是唯一调用函数

之后,有人呼吁我继续工作,但会被关闭

推荐答案

首先,注意你的 gc_probability gc_divisor 。如果两者都设置为1,这意味着,该垃圾收集器(GC)处理开始在每个会话初始化的概率 gc_probability / gc_divisor = 1/1 = 1 ( 100%)。

First, watch out your gc_probability and gc_divisor. If both are set to one, that means that the probability that the garbage collector (GC) process is started on every session initialization is gc_probability / gc_divisor = 1/1 = 1 (100%).

您可以把它的默认值,或给它一个较高的数量,以减少的GC被调用的机会。

You could leave it to the defaults or give it a higher number in order to reduce the chance of the GC being called.

例如:

session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
        cookie_lifetime: 600 # Ten minutes
        gc_probability: 1
        gc_divisor: 10000

另外,如果您使用的是虚拟机,检查服务器的日期,所产生的会话cookie将与的到期时间时间()+ cookie_lifetime 其中时间取自服务器

Also, if you're using a Virtual Machine, check the date of your server, the resulting session cookie will be stamped with an expiry time of time() + cookie_lifetime where the time is taken from the server.

有可能是可能的是,如果该服务器有一个坏日期,该cookie将inmediately到期。试想一下:服务器日期 2015年1月31日,您的浏览器 2015年2月1日。服务器发送的cookie到期 2015年1月31日晚上11点,您的浏览器接收一个​​cookie与已通过的到期日期。

It could be possible that, if the server had a bad date, the cookie would expire inmediately. Imagine: server date 2015-01-31, your browser 2015-02-01. Server sends cookie that expires on 2015-01-31 at 11pm, your browser receives a cookie with an expiration date that has already passed.

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

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