使用AJAX时,检查会话超时 [英] checking session timeout when using AJAX

查看:150
本文介绍了使用AJAX时,检查会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ColdFusion页面的用户可以打开一个模式,并查看数据行的详细信息。但是,如果用户坐在页面长于缺省为20分钟的会议超时时,它抛出一个错误,因为它是寻找会话变量,并不能找到它们。我知道如何为这个陷阱与服务器端的code,但我似乎无法让AJAX调用成功确定是否会依然存在。

I have a ColdFusion page were a user can open a modal and view more information about a row of data. However if the user sits on the page longer than the default 20 minute session timeout, it throws an error because it's looking for the session variables and can't find them. I understand how to trap for this with server side code, but I can't seem to get the AJAX call to successfully determine if the session still exists.

下面是火,当用户点击按钮,打开模态的AJAX code。基本上,它检查是否有在CFC函数存在会话。我的问题是,它总是返回有效。

Here's the AJAX code that fires when the user hits the button to open the modal. Basically it's checking if the session exists with a function in a CFC. My problem, is that it always returns 'valid'.

//this checks if the session is expired
function checkSessionExists() {
    $.ajax({
        //this is the that has function
        url: 'components/Admin.cfc',

        //POST method is used
        type: "POST",
        //pass the data        
        data: {
            method: "checkSessionExists"
            },
        async:   false,
        success: function(response) {

            //$('#loading').hide();
            var obj = $.trim(response);

            if (obj == 'expired') { //it's never expired
                alert('Sorry, your session has expired.')
                window.location.href = "logout.cfm";
                return false;
            }

            else{

            }
        },
        error: function(jqXHR, exception) {
            alert('Uncaught Error.\n' + jqXHR.responseText);   
        }
    });
return false;
}

下面是在CFC功能:

<cffunction name="checkSessionExists" access="remote" output="false" returntype="string" returnformat="plain" hint="I check if session is expired."> 

    <cfif NOT structKeyExists(session, "alive")>
        <cfreturn "expired" />
    <cfelse>
        <cfreturn "valid" />
    </cfif>

</cffunction>

我在想,当我问CFC会话是否存在,它仍然有会话变量,因为页面没有刷新过二十分钟。所以...想知道我怎么会发一个AJAX请求到CFC并在CFC中的功能可按重新评估会话变量。任何帮助将是AP preciated感谢!

I'm thinking that when I ask the CFC if the session is there, it still has the session variables because the page hasn't refreshed in over twenty minutes. So... wondering how I would send an AJAX request to a CFC and have a funciton in the CFC re-evaluate the session variables. Any help would be appreciated thanks!

推荐答案

我要取决于你如何设置您的session.alive值。请记住,你的code将返回有效只要活着是present开庭。如果session.valid是建立在会话初始化code,you'l可能会出现这样的反应。

I'll depend on how you set up your session.alive value. Bear in mind that your code will return 'valid' as long as alive is present in session. If session.valid is set up in session init code, you'l get the response you're seeing.

任何要求一个.CFM会导致会话超时值复位为()+会话持续时间。我用这个在过去:

Any request to a .cfm will cause the session timeout value to reset to now()+session duration. I've used this in the past to :

  1. 倾听KEYUP或点击页面上的事件。如果有这样的事件发生,设置一个布尔标志
  2. 每隔几分钟,检查值发送Ajax调用来保持会话处于活动状态,如果该值是真实的。然后,将变量设置为false

然后重复循环。这意味着,如果用户与网站互动,他们的会话将保持开放,他们将不会被抛出在他们在做什么结束。如果您的企业/网站的会话策略允许的话,这可能是一个非常不错的设置。

Then and the cycle repeats. It means that if your users are interacting with the site, their session will be kept open and they won't get thrown out at the end of what they were doing. If your business/site session policy allows, this can be a very nice setup.

这篇关于使用AJAX时,检查会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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