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

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

问题描述

我有一个 ColdFusion 页面,用户可以打开一个模式并查看有关一行数据的更多信息.但是,如果用户在页面上停留的时间超过默认的 20 分钟会话超时,则会引发错误,因为它正在查找会话变量但找不到它们.我了解如何使用服务器端代码来捕获此问题,但我似乎无法让 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 代码.基本上,它使用 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.
' + jqXHR.responseText);   
        }
    });
return false;
}

这是 CFC 中的函数:

here's the function in the 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 会话是否存在时,它仍然有会话变量,因为页面在 20 分钟内没有刷新.所以...想知道我将如何向 CFC 发送 AJAX 请求并在 CFC 中具有重新评估会话变量的功能.任何帮助将不胜感激!

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 值.请记住,只要会话中存在活动,您的代码就会返回有效".如果 session.valid 在会话初始化代码中设置,您将得到您所看到的响应.

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 的任何请求都会导致会话超时值重置为 now()+会话持续时间.我过去用它来:

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. 监听页面上的按键或点击事件.如果发生任何此类事件,请设置一个布尔标志
  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天全站免登陆