Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Killing a session on a closed browser

Status
Not open for further replies.

cfdeveloper

Programmer
Nov 20, 2003
144
GB
Hey all,

Is there a way through JavaScript or some other resource to detect when the
browser window is closed so I can end a session?

The problem is, this can only happen when the browser is closed, most of the
JavaScript commands (like onunload) also activate when the browser is
opened, refreshed, or the back/forward button is pressed.

I can't kill sessions when those things happen, only when the browser is
closed.

Any help is appreciated

Thanks,
cfcoder
 
Can you not close the session when the page is closed? Would that work for you.
 
"page is closed"? you mean when the user closes the browser? That is what I'm trying to do, to delete all session variables when the user closes the browser. I've got this javascript code from somewhere.

<script language=&quot;javascript&quot;>
if (document.cookie != &quot;&quot;) {
thisCookie = document.cookie.split(&quot;; &quot;)
expireDate = new Date
expireDate.setDate(expireDate.getDate()-1)

for (i=0; i< thisCookie.length; i++) {
cookieName = thisCookie.split(&quot;crumb&quot;)[0]
document.cookie = cookieName + &quot;=;expires=&quot; + expireDate.toGMTString()


}
}</script>

I put this code in the application.cfm file, this does the trick but I want this code to only execute when the browser is closed
 
Does something like this do what you are looking for?

<html>
<head>
<script language=&quot;javascript&quot;>
function DeleteSession()
{
alert(&quot;User your structdelete() function here&quot;);
}
</script>
<title>Untitled Document</title>

</head>

<body onUnload=&quot;DeleteSession();&quot;>

</body>
</html>
 
Yes a refresh is an unload event. You should create the sessions that you want at the top of the page and delete them on unload. That way the session will be available when the page is open and not when it is closed.

Hope this helps

Wes
 
If that doesn't work you could open a session at the top of the page like <cfset session.IsOpen = &quot;true&quot;> then delete it on unload. That way you will know if the page is open or not so you can do whatever you want with the other session variables if you need to keep their values if the user refreshes.

Hope this helps

Wes
 
Here, this is Macromedia's answer on the subject. Place the following code in your application.cfm file, and whenever a browser is closed it will kill the session.
Code:
<cfif isdefined(&quot;cookie.CFID&quot;) and isdefined(&quot;cookie.CFTOKEN&quot;)>
    <cfset tempCFID = cookie.CFID >
    <cfset tempCFTOKEN = cookie.cftoken >
    <cfcookie name=&quot;CFID&quot;  value=&quot;#tempCFID#&quot; >
    <cfcookie name=&quot;CFTOKEN&quot;  value=&quot;#tempCFTOKEN#&quot; >
</cfif>



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
thank you all for your replies. I am using client variables to keep the values persistent across pages.

<CFCOOKIE name="CFID" value="#Client.CFID#">
<CFCOOKIE name="CFTOKEN" value="#Client.CFTOKEN#">

As you can see the above code stores the cfid and cftoken values in cookies and because there is is no "expires" attribute, the cookie is stored in the memory and expires when the browser session ends. Hope this is making sense

Regards,
cfcoder
 
Exactly what i do too

with a little cfif:

<cfif IsDefined("FORM.Keepalive")>
<cfcookie name="UID" value="#getuser.User_ID#" expires="NEVER">

<cfelse>
<cfcookie name="UID" value="#getuser.User_ID#">
</cfif>

kills the cookie on window close if keepalive is not checked during login. otherwise cookie stays. works great. simple.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top