• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Detecting end of session with JS

New Here ,
Sep 09, 2016 Sep 09, 2016

Copy link to clipboard

Copied

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

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 09, 2016 Sep 09, 2016

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2016 Sep 10, 2016

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

@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 ...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

marcb301158 wrote:

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

Then Application.cfc is much better for you than Application.cfm. Something like this:

Application.cfc

<cfcomponent displayname="Application file">

    <cfscript>

        this.name = "myapp";

        this.applicationTimeout = "#createTimespan(2,0,0,0)#";

        this.clientManagement = "true";

        this.sessionManagement = "true";

        this.sessionTimeout = "#createTimeSpan(0,0,30,0)#";

        this.scriptProtect = "all";

    </cfscript>

   

    <cffunction name="onSessionStart">

        <cfset session.validlog = -1>

    </cffunction>

    <cffunction name="onSessionEnd">

       <cfargument name = "SessionScope" required="true">

       <cfargument name = "AppScope" required="true">

          <cfset session.validlog = -1>

    </cffunction>

</cfcomponent>

-------------------------------------------------

login.cfm

<!--- Validate login credentials --->

<cfif isLoginValid>

<cfset session.validlog = 1>

</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2016 Sep 12, 2016

Copy link to clipboard

Copied

​Hello,

Thanks for the answers.

From where is "isLoginValid​" coming from ? Is it a CF function ?

In my Application.cfm file i've something like this :

How can i insert these param in the cfc file ?

<cfset application.vmware = "#CGI.SERVER_NAME#">

<cfset application.mailStb = stb@mail.com">

<cfset oldlocale = SetLocale("French (Belgian)")>

<cfswitch expression="#application.vmware#" >

<cfcase value="DevServer">

<cfset TableMvt = "[CF].[dbo].[HOLT_MOUVEMENTS_WORK]" >

<cfset TableEqt = "[CF].[dbo].[HOLT_EQUIPEMENTS_WORK]" >

<cfset VueGlobale = "[CF].[dbo].[VW_HOLTERS_GLOBAL_WORK]" >

<cfset EnvDev = TRUE >

</cfcase>

<cfcase value="ProdServer">

<cfset TableMvt = "[CF].[dbo].[HOLT_MOUVEMENTS]" >

<cfset TableEqt = "[CF].[dbo].[HOLT_EQUIPEMENTS]" >

<cfset VueGlobale = "[CF].[dbo].[VW_HOLTERS_GLOBAL]" >

<cfset EnvDev = FALSE >

</cfcase>

</cfswitch>

Thx

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 13, 2016 Sep 13, 2016

Copy link to clipboard

Copied

Marc,

First, I would be remiss if I failed to mention that application.cfm is the old school approach to ColdFusion, and should be replaced with application.cfc, as the cfc is more modern, easier to work with, and you can set up all kinds of things with it.

But back to your most recent question.  It can be placed almost anywhere within your application.cfm, but would probably be best right after the last line of code you provided, after the CFSWITCH.  It won't be using JavaScript to detect end of session, per your original request, however it would be the best practice for checking end of session.

V/r,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 13, 2016 Sep 13, 2016

Copy link to clipboard

Copied

LATEST

marcb301158 wrote:

From where is "isLoginValid" coming from ?

IsLoginValid is simply a boolean variable. (I expected you to recognize it by its name and to translate it according to your own context.) It holds the result of the validation of the user's credentials. If username and password are valid, then isLoginValid true, else false.

The variables seem to me to have scopes as shown below. I don't expect you to change servers or locale once the application has started.

Application:

vmware

mailStb

oldlocale

tableMvt

TableEqt

vueGlobale

envDev

Session:

validlog

isLoginValid (or your own equivalent variable)

You could then have

Application.cfc

<cfcomponent displayname="Application file">

<cfscript>

this.name = "myapp";

this.applicationTimeout = "#createTimespan(2,0,0,0)#";

this.clientManagement = "true";

this.sessionManagement = "true";

this.sessionTimeout = "#createTimeSpan(0,0,30,0)#";

this.scriptProtect = "all";

</cfscript>

<cffunction name="onApplicationStart" returntype="boolean">

<cfset application.vmware = CGI.SERVER_NAME>

<cfset application.mailStb = "stb@mail.com">

<cfset application.oldlocale = setLocale("French (Belgian)")>

<cfswitch expression="#application.vmware#" >

    <cfcase value="DevServer">

        <cfset application.tableMvt = "[CF].[dbo].[HOLT_MOUVEMENTS_WORK]" >

        <cfset application.tableEqt = "[CF].[dbo].[HOLT_EQUIPEMENTS_WORK]" >

        <cfset application.vueGlobale = "[CF].[dbo].[VW_HOLTERS_GLOBAL_WORK]" >

        <cfset application.envDev = TRUE >

    </cfcase>

    <cfcase value="ProdServer">

        <cfset application.tableMvt = "[CF].[dbo].[HOLT_MOUVEMENTS]" >

        <cfset application.tableEqt = "[CF].[dbo].[HOLT_EQUIPEMENTS]" >

        <cfset application.vueGlobale = "[CF].[dbo].[VW_HOLTERS_GLOBAL]" >

        <cfset application.envDev = FALSE >

    </cfcase>

</cfswitch>

<cfreturn true>

</cffunction>

<cffunction name="onSessionStart">

<cfset session.validlog = -1>

</cffunction>

<cffunction name="onSessionEnd">

<cfargument name = "SessionScope" required="true">

<cfargument name = "AppScope" required="true">

<cfset session.validlog = -1>

</cffunction>

</cfcomponent>

-------------------------------------------------

login.cfm

<!--- Validate login credentials. isLoginValid is a boolean variable that is true if the login credentials are valid, false otherwise. --->

<cfif isLoginValid>

<cfset session.validlog = 1>

</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation