Skip navigation
Currently Being Moderated

Session variable not being set immediately after CFSET session.var = "data"

Jul 27, 2012 6:19 AM

Hello, everyone.

 

Got a real head scratcher (at least, for me it is.)

 

Just experimenting a little bit, this isn't project related (yet), but something I'd like to code as reusable for future projects.

 

I'm attempting to write code that will clear session variables when the browser is closed, -AND- disassociate the session variables from the client by altering and expiring the cfid and cftoken cookies.

 

In my Application.cfc, I have the following:

 

onSessionStart:
 <cfif NOT StructKeyExists(session,"app")><cfinvoke component="components" method="setDirs" returnvariable="session.app"></cfif>
 <cfset oRequest = getPageContext().getRequest()>
 <cfset session.cookies = oRequest.getHTTPRequest().getCookies()>
 <cfcookie name="cfid" value="#session.cfid#">
 <cfcookie name="cftoken" value="#session.cftoken#">
 
 onSessionEnd:
 <cfloop index="local.cookiename" list="cfid,cftoken,cfmagic"><cfcookie name="#local.cookiename#" value="" expires="now"></cfloop>

 

In the page that loads, I have:

 

<cfset session.cookies = getPageContext().getRequest().getHTTPRequest().getCookies()>
 <cfoutput>
 <cfloop index="idx" array="#session.cookies#">#idx.getName()# = #idx.getValue()# - Expires: #idx.getMaxAge()#</cfloop>
 </cfoutput>

The above is where the error message "Cookies undefined in Session" appears.  Didn't I just set session.cookies, twice?

 

Thank you,

 

^_^

 
Replies
  • Currently Being Moderated
    Jul 27, 2012 6:30 AM   in reply to WolfShade

    I don't quite follow what you're doing, but you can't mess with cookies in onSessionEnd() because it - intrinsically - is not associated with a request... and you can't send cookies to the browser other than as part of a response to a request.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 7:10 AM   in reply to WolfShade

    You can't.

     

    To change a browser's cookies, you need to communicate with the browser.  This is done when the CF server sends the HTTP response to a request from that browser.

     

    onSessionEnd() - by its very nature - only runs when there isn't communcation with the browser, and hasn't been any communication from the browser for at least the [session timeout] period.  So you can monkey with the cookie scope all you like in there, but the changes are never sent to the browser, because the browser has long since "left the building" as it were.

     

    As HTTP is connectionless, and the browser doesn't communicate with the CF server anyhow (the browser only talks to the web server), there is no way for the CF server to know that the client has closed their browser.  All that takes place on the client machine, and CF is sitting on the CF server.  Even the web server doesn't know what's happening on the client UI.

     

    What you can do is to set the cookies and the session to have very short timeouts, so that the expire quickly once there's no activity to refresh them.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 7:21 AM   in reply to WolfShade

    Yes, that's pretty much it. That's also the right way to handle session cookies.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points