By: Chris

This seems like a common question in the forums so I thought I would turn it into a tutorial.

"Thanks to Pablo for the code."

Put this in your application.cfm page:
<!--- routine to clear session when browser closes || This will ensure that the person is logged in correctly when they return --->
<cfif IsDefined("Cookie.CFID") AND IsDefined("Cookie.CFTOKEN")>
  <cfset Variables.cfid_local = Cookie.CFID>
  <cfset Variables.cftoken_local = Cookie.CFTOKEN>
  <cfcookie name=
"CFID" value="#Variables.cfid_local#">
  <cfcookie name=
"CFTOKEN" value="#Variables.cftoken_local#">
</cfif>

The code above will will log them out when they close the browser.

About This Tutorial
Author: Chris
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX7
Total Views: 23,777
Submission Date: July 18, 2006
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • Miquel, There is no way to actually execute a session ending without using Application.cfc... This is the only real way to do it and that is to no longer associate the user with the previous session.. in essence it does the same thing... you just can't do cleanup afterwards. That is why Application.cfc is vital to make your applications run smoother and cleaner.

  • Does the code kill the session, or does it no longer associate that visitor with his/her previous session?

  • "Comment: Sorry, but the above code does not do anything of the sort.All it does it copy the cookie into local variables and then back again, on every single page load. You appear to have completely missed something. There is no code to detect when the browser is closed and no code to delete the session variables." Notice that the cookies that get set; have no expiration.. those cookies will expire on browser close... to technically next time you come your session with be a new session. So yes it does work as it achieves what you are looking to do.. kill the session on browser close.

  • Ok, since I had a lot of confusion on the last tutorial, I decided to rewrite it. "Thanks to Pablo for some of the code." First, we will create an Application.cfm with the following: Application.cfm Then we will create a page to set the session set_session.cfm Then we will create a page to display the results. test.cfm #session.theTest# Now open your browser and go to set_session.cfm. If you close the browser and go back to test.cfm, the session is kept. Now let’s modify the code. Application.cfm set_session.cfm test.cfm #session.theTest# No session sert. Now close your browser and go to test.cfm. The session is no longer defined.

  • I have asked Pablo to delete this tutorial and I will look into writing another that is more clear. Sorry for the confusion.

  • Well we can all rip code from a forum and submit it as a tutorial sigh. Perhaps someone should tell him that you need to use some javascript to detect when the browser closes and then delete the session and cookies.

  • Here is a topic in the forums that I think this tutorial was derived from. It gives further explanation. http://www.easycfm.com/forums/viewmessages.cfm?Forum=12&Topic=9587

  • This tutorial needs to be axed.

  • Sorry, but the above code does not do anything of the sort. All it does it copy the cookie into local variables and then back again, on every single page load. You appear to have completely missed something. There is no code to detect when the browser is closed and no code to delete the session variables.

  • How will this code "clear" the session (from memory)? You really need to explain this in order for this post to qualify as a tutorial. All I can see is some code that, if you're lucky, sets a persistent cookie.

Advertisement

Sponsored By...
Powered By...