Skip to main content
Inspiring
January 19, 2010
Answered

define user vars in session

  • January 19, 2010
  • 1 reply
  • 871 views

Hi all,

I am trying to set some session.user.* variables when user logs in and it works somewhat.  When user first logs in it works fine but if session expires and user logs in again then it doesn't work.  User would have to logout manulay in order for it to work again.  I am using j2ee for session management.

I've also tried doing the user session set in the onSessionStart but that didn't work.  Here is my code:

<cffunction name="OnRequestStart" >
  <cfargument name = "request" required="true"/>
  <cfif IsDefined("URL.logout")>
    <cflogout>
    <cflocation url="/index.cfm" addtoken="no">
  </cfif>
  <cflogin>
  <cfif NOT IsDefined("cflogin")>
    <cfinclude template="loginform.cfm">
    <cfabort>
    <cfelse>
    <cfif cflogin.name IS "" OR cflogin.password IS "">
      <cfoutput>
        <h2>You must enter text in both the User Name and Password fields.</h2>
      </cfoutput>
      <cfinclude template="loginform.cfm">
      <cfabort>
      <cfelse>
      <cfquery name="loginQuery" dataSource="#Application.DNS#">
            SELECT UserID, UserLevel, news, calendar, site, marketing, account, users
            FROM Users
            WHERE
               UserID = '#cflogin.name#'
               AND Password = '#cflogin.password#'
            </cfquery>
      <cfif loginQuery.UserLevel NEQ "">
       <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#" roles="#loginQuery.UserLevel#">
        <cfscript>
             session.user.news = loginQuery.news;
             session.user.calendar = loginQuery.calendar;
             session.user.account = loginQuery.account;
             session.user.site = loginQuery.site;
             session.user.marketing = loginQuery.marketing;
             session.user.users = loginQuery.users;
     </cfscript>
        <cfelse>
        <cfoutput>
          <H2>Your login information is not valid.<br>
            Please Try again</H2>
        </cfoutput>
        <cfinclude template="loginform.cfm">
        <cfabort>
      </cfif>
    </cfif>
  </cfif>
  </cflogin>
</cffunction>

    This topic has been closed for replies.
    Correct answer Joshua_Cyr

    And that is when they try to log in, or if the session times out then they visit another page?


    1 reply

    Participating Frequently
    January 19, 2010

    When you say 'doesn't work'  what do you mean?  Does it just go to the login form again?  Or does it show an error about missing session vars?  Or something else?

    TiGGiAuthor
    Inspiring
    January 19, 2010

    sorry I didn't clerify that, my pages error our due to missing session.user. variables.

    Joshua_CyrCorrect answer
    Participating Frequently
    January 19, 2010

    And that is when they try to log in, or if the session times out then they visit another page?