Search code examples
coldfusioncoldfusion-8

How to test for live session variable and refresh via JavaScript if not live?


I am using ColdFusion 8 and jQuery.

I have a page that uses a lot of ajax. When a button on the page is clicked, some data is sent to the CFC to retrieve data. The data retrieval requires session variables. If the page has sat unused for a while and the session has ended, the page creates an error. The user can click all he wants but won't see the error because it's behind the scenes in ajax. Firebug shows me that the session variable no longer exists. Here's the error:

Element USERINFO.CATALOG_ID is undefined in SESSION.

At the top of my CFC, I move things from the APPLICATION and SESSION scopes to the VARIABLES scope like this:

<cfcomponent>

    <cfscript>
    // DEFAULT DATASOURCE
    VARIABLES.DS = APPLICATION.Datasource;
    // SET CATALOG AND MARKET
        VARIABLES.Catalog_ID = SESSION.UserInfo.Catalog_ID;
    VARIABLES.CatalogType = THIS.getCatalogType(VARIABLES.Catalog_ID);
    </cfscript>

    <cffunction name='SomeFunction>
    </cffunction>

<cfcomponent>

What is a good way to test if the session is still active and if not, return something to the page instructing it to refresh itself thereby restarting this session?


Solution

  • If I want to keep a session open I use a very small (1 pixel) iframe. The page in the iframe simply does a meta refresh every x seconds and this keeps the session alive.

    Having said that, it's rare that I want to keep a session alive. They timeout for a reason. We also incorporate a javasript countdown timer that directs the user to another page at about the same time the session times out. In the scenario describe above, the user would not be able to click the button because it wouldn't be there.