Skip to main content
marcb301158
Participant
September 9, 2016
Question

Detecting end of session with JS

  • September 9, 2016
  • 2 replies
  • 1223 views

Hello,

i'm using CF11,

part of my application.cfm file :

<cfapplication name="myapp" clientmanagement="yes" sessionmanagement="yes" sessiontimeout="#createtimespan(0,0,30,0)#">

When the user is logged in, session variable session.validlog is set to 1

The JS script (loaded with index.cfm) :

<script type="text/javascript">

  function session_checking()

  {

     $.post("ajax-session.cfm", function(data) {

         if(data === "-1")

         {

             window.location = "http:/myserver/login.cfm";

         }

         else

          {

          console.log('Session active') ;

         }

     });

  }

  var validateSession = setInterval(session_checking, 5000);

  </script>

the ajax-session.cfm file :

<cfswitch expression="#session.validlog#" >

  <cfcase value="1">1</cfcase>

  <cfdefaultcase>0</cfdefaultcase>

</cfswitch>

So : every 5 sec ajax-session.cfm is executed, if it return -1, user is redirected to the login page.

Issue :

When application session timeout is coming to 0, the user is not redirected to the login page ...

Any idea ?

Thanks,

Marc

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    September 10, 2016

    marcb301158 wrote:

    When the user is logged in, session variable session.validlog is set to 1

    To repeat WolfShade's point, what is the value of session.validlog when the user is not logged in, 0 or -1?

    if(data === "-1")

    {

    window.location = "http:/myserver/login.cfm";

    }

    ...

    ...

    <cfswitch expression="#session.validlog#" >

    <cfcase value="1">1</cfcase>

    <cfdefaultcase>0</cfdefaultcase>

    </cfswitch>

    Use

    session.validlog = -1 or 1

    if(data == -1)

    <cfdefaultcase>-1</cfdefaultcase>

    or

    session.validlog = 0 or 1

    if(data == 0)

    <cfdefaultcase>0</cfdefaultcase>

    marcb301158
    Participant
    September 12, 2016

    @Wolfshade :

    Modifying ajax-session.cfm with #session.validlog#

    When session ended, nothing happend, no return to login page.

    In the console i see the output looping with the value of #session.validlog#.

    It seems that the session never end.

    When i comment the script, session ended as well.

    Wierd ...

    @BKB : answer in the above response session.log value is always 1 (session active) even if session timeout is out ...

    WolfShade
    Legend
    September 12, 2016

    Running the script every five seconds is "polling", and (dare I say it) totally unnecessary.  Since the application.cfc/.cfm is run prior to every page load, you can set a conditional (if/else or switch/case, I prefer the latter) in your onRequestStart() that checks for session.

    However, the conditional will loop eternally unless part of the conditional takes the current page into account.  So instead of JUST doing "if this session variable doesn't exist, redirect to login page", you need to "if this session variable doesn't exist AND if this page isn't login.cfm, redirect to login.cfm".  That way, if the redirect goes to login.cfm, application.cfc/.cfm doesn't keep redirecting endlessly.

    HTH,

    ^_^

    WolfShade
    Legend
    September 9, 2016

    Kind of a clever combining of CF and JS.  Very nice.

    However, you're expecting -1 if the session is gone, but your defaultcase is returning 0, not -1.

    Why not just return the #session.validlog# value instead of using a switch/case?  It's just returning a hardcoded version of #session.validlog#, anyway.

    V/r,

    ^_^