Question

How do I clear all session variables once browser is closed.

Asked by: marhk51

Hi,

How do I clear all of session variables once the user has closed the browser window ???

Many thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-06-02 at 12:35:42ID20634056
Tags

session

,

clear

,

variables

Topic

ColdFusion Application Server

Participating Experts
5
Points
50
Comments
20

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. session Variables and clearing
    What's the story with session variables(EG. Session("var") = strSQL)? EG. Do I need to clear these or reset them to 0 when I've finished with 'em? Like one should with global variables???
  2. Session variables
    Hi 1) Is it true that a session end only when user close the browser ? As such, all session variables are destroy & clear but system automatically, right ? Otherwise, when can I clear the session variables ? 2) How about view state variables ? when is it clear & de...
  3. Clearing selected Session Variables
    I am working on a web application that uses a number of session variables (this cannot be changed, so please do suggest doing so). At the end of a certain process, I would like to remove all session variables from the collection with certain exceptions. I have tried the code ...
  4. Session Variable
    Hello, In my asp.net page on logout button click, i used session.abandon statement. but when I logout and login once again, the same session variables are being used. why is that? -- praveen

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: netuser1976Posted on 2003-06-02 at 14:26:58ID: 8632991

<cfset tmp=StructClear(Session)>

 

by: entrance2002Posted on 2003-06-02 at 18:48:08ID: 8634538

You may refer to this site http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_20114864.html.

Since, this is similar or related to your posted problem but it's not my answer.

I do hope that I have helped you find a solution.

GOODLUCK!

Entrance2002 :)

 

by: marhk51Posted on 2003-06-03 at 00:47:39ID: 8636247

NETUSER1976:

Where do I add "<cfset tmp=StructClear(Session)>" into my code, as this will clear all of variables a soon as it is issued, won't it. I only want the session variables to be cleared after the browser is closed.

Entrance2002:

Thanks for that link, but it seems to be for session cookies. I'm not using cookies.

 

by: raj_Posted on 2003-06-03 at 05:19:42ID: 8637853

u can include something like this in the onlogout.cfm page, or however u are handling the      logout ooperation..

<cfscript>
if (isdefined("session.sessionVar1"))
      {
            structdelete(Session, "sessionVar1");
      }
      
// list all the session variables here
</cfscript>


having said this i am sure most of the guys here will agree with me that the user will never ever hit the logout link, but would rather hit the close window X. so, u may want to optimise the location and instance where u want to call this session clearing script...

typically, i check for the session varauble, if found all is ok, else, i re-route the user back to the login page and before he logs in, i will clear all the session variables, just as a good housekeeping practise..

cheers
-r

 

by: marhk51Posted on 2003-06-03 at 06:14:33ID: 8638362

raj_

Thanks for your reply.

The problem that I am having is that users are logging in and then when they are finished they are not logging out they are just hitting the 'X', so when another user comes along and goes into the same site the previous person is still logged in, which is a security risk.

So how do I go about clearing the session variables once the browser window has been closed, because if I add a script which checks if the variables exist, how do I know if the variables are from the new users session or the old users session ???

Thanks

 

by: raj_Posted on 2003-06-03 at 06:24:02ID: 8638468

well, if all u have is a single window application, where u do not do any fancy pop-ups etc, try using the onunload() function.. this gets called everytime a window gets closed... this can be used to call a clearSessionVariableds.cfm template which can do the clean up.. or try the onRequestEnd.cfm.. this is the last file which gets called (has some limitations)

cheers
-r

 

by: marhk51Posted on 2003-06-03 at 07:09:30ID: 8638834

raj_

I can't use th OnRequestEnd.cfm file as the limitations it has will get in the way and won't work, and I am not too good with Javascript so if you could show me how to use the onUnload() function to load a CFM template, I would be very greatful. Please make sure that the template is run in the background and not opened in a new window.

Many Thanks.

 

by: raj_Posted on 2003-06-03 at 07:36:00ID: 8639056

hmm.. i have never tired using this.. hence UNTESTED ;-)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
<script>
      function foobar()
      {
            document.all.cleaner.src='cleanUpMySessionVars.cfm';
      }
</script>
</head>

<body onunload="foobar()">
      my page comes here !!!
      
      <!--- use a iframe for executing the session cleaner script--->
      <iframe name="cleaner" id="cleaner" width="95%" height="650" frameborder="0" style="display: none"> </iframe>
      
</body>
</html>

 

by: marhk51Posted on 2003-06-03 at 07:45:10ID: 8639157

_raj,

I just get a 'Object Expected' error in IE.

P.S What the hell is a iframe ?

 

by: raj_Posted on 2003-06-03 at 07:57:02ID: 8639283

lol.. iframe or inline frame is a blessing to do some cool things in IE.. think of them as the good ol frames, but a floating version..
i may be wrong with the name resolution "document.all.cleaner.src".. I will try and see where i am screwing up..

the page in simple english being:

1) <body onunload="foobar()"> :: this unload method will be called when the page is closed.. this way the function foobar() gets called
2) function foobar, will run a page called as cleanUpMySessionVars.cfm, which u have, which basically is a series of
<cfscript>
if (isdefined("session.sessionVar1"))
    {
         structdelete(Session, "sessionVar1");
    }
   
// list all the session variables here
</cfscript>
for all the session variables used
3) the iframe acts like a container which is used to run the file..

cheers
-r

 

by: raj_Posted on 2003-06-03 at 09:07:51ID: 8640000

i guess i must have succeeded in shooting myself in the foot..  on deeper thought,. this may have to be modified.. the function foobar() actually needs to open a small window on the extreme left or wherever, basically away from the users sight (or viewing scope).. this will then run the clear session variables page which u have..

the reason for this being (i may be wrong) is that on the onunload method being called, the connection between the server and client is lost and hence u cannot execute the clear session page inside the iframe..

cheers
-r

 

by: raj_Posted on 2003-06-03 at 09:45:05ID: 8640364

ok after a quick lunch, blood flows back to the cranium and things fall in place..

let me give u a small insight as to how the session variables are stored (i am sure u must be aware of this).. sessions like many other variables are stored in structures in coldfusion, this structure is identified by the structid which is a combination of the cfid and cftoken, which are unique for each browser, hence each user using different browser has his own copy of the session variables..
now the cfid and cftoken are stored in cookies and hence if u delete the cookies or expire them, u should be fine..

try this..
1) login to ur app
2) close the brower window
3) reopen the app immediately, u will bypass the login page - as the sessions are alive..
now
5) close the app window
6) open the D:\Documents and Settings\Cookies directory or wherever the cookies are stored on ur PC, identify the one which is created by ur app
delete that cookie
7) reopen the app, it wil make u to re-login. in this case the session is still alive, ad will be till it times out, but the browser is unable to identify which structure to use.

so, try expiring the cookie, using the a function called from the  onUnload method and u should be able to get the desired result..

cheers
-r

 

by: marhk51Posted on 2003-06-03 at 10:14:35ID: 8640648

_raj,

Hope you enjoyed lunch, because this is looking to be a long day ;)

Right, I know what you're getting at but i am not using COOKIES so there ain't any to delete.

So the scenario is still the same, sorry !

Back to you mate... ;)

 

by: substandPosted on 2003-06-03 at 14:38:36ID: 8642968

you can't do what you are trying.  there is no "onwindowclose" event, and for good reason- if someone wanted you to never be able to close a window, they could do that.  in fact, i don't even like the idea of the onunload event, because they can do the same thing if they want, but opening thier page in a new window.

the best you can do is use the onUnload event to call a page to expire the session, but then pass the userinfo to the new page (in case it is in your site) to make the user "relogin".  if they closed the window, they won't login again.  if they went to another site, they won't log in again.  this still gives the problem that you are potentially passing info to other sites.

if you were using cookies, you could just leave off the expires property of the cookie, and it would delete itself when the browser closed.  but you would still have the problem of if they leave the site and don't logout, someone else on the same machine can go back and be logged in.

if the onunload doesn't work for you, i think you're sol.   if the data is so sensitive that this is an absolute neccesity, you would hope the people who are using it know the sensitivity of the data, and aren't enough of an idiot to log on at the local library and leave the nation's most top secret documents related to nuclear arms open for the world to see.  if they do wish to be that insecure with thier own data, then you would hope they would just spend the money to create a technology and an application that does what they are looking for.

if the onunload event is what you're looking for, you'd need to do something like:

application.cfm:

if isdefined("form.login") and cgi.http_referrer is something from yoursite
   attempt to authenitcate user
if user is not authenticated, <cfabort>
else continue as normal


every other page:
<script>
function logoutandlogin()
{
location.href('logout.cfm?gourl='+hide.innerHTML);
}
</script>

<body onunload="loginandlogout();">
<div name="hide" id="hide">
 
</div>

and every link should look like:
<a href="#" onclick="hide.innerHTML='test.cfm';logoutandlogin();">click here</a>

with test.cfm replaced as the url to where you want the link to go.



on logout.cfm:

set temp variables of the session
expire the session
create and post a form (using formname.submit();) with the temp variables to the page #url.gourl#

application.cfm will then log them in again.

note that certain versions of netscape will not allow you to autopost a form like that, so you might just use <cflocation>

also, besure to check if #gourl# is defined... this also will not allow them to visit other sites in this window. (since the onunload will call your function)












 

by: substandPosted on 2003-06-03 at 14:39:16ID: 8642975

also, i've read that onunload only works in IE, though i have not tested it myself.

 

by: marhk51Posted on 2003-06-04 at 00:54:06ID: 8646122

Right,

Well it looks like the Unload() option is a last resource, it's going to be a pain in the arse :-)

If you don't mind, how do i do the following:

//-->
Using cookies in the same way as the session variables;
To check if cookies are enabled;
Make cookies expire when browser is closed;
Make cookies never expire;

Also in the new IE browsers if you try using cookies, a browser with standard security will block my cookies because as they don't have a privacy policy, how do I go about that ??

Many Thanks.

 

by: iwordsPosted on 2003-06-06 at 08:53:01ID: 8667226

There's an easier way to force the session to terminate when the user closes the browser window.  Stick this in your application.cfm...

<cfif IsDefined("Cookie.CFID") AND IsDefined("Cookie.CFTOKEN")>
      <cfset cfid_local = Cookie.CFID>
      <cfset cftoken_local = Cookie.CFTOKEN>
      <cfcookie name="CFID" value="#cfid_local#">
      <cfcookie name="CFTOKEN" value="#cftoken_local#">
</cfif>

 

by: marhk51Posted on 2003-06-06 at 09:04:02ID: 8667330

Thanks iwords, it worked a treat !

 

by: substandPosted on 2003-06-06 at 11:22:02ID: 8668506

i thought you weren't using cookies?

 

by: substandPosted on 2003-06-06 at 11:25:44ID: 8668531

anyway, glad you got it fixed.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...