# Setting session to nil when window is closed

**URL:** <https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075>\
**Category:** Rails\
**Created:** [October 29, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075 "2008-10-29T21:22:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/1 "2008-10-29T21:22:50Z")

</div>

I have this app at work that has Administrators and Users as two types  
of users. I know who is a of what type by setting session variables:  
session[:admin] and session[:user] holding the id’s. The administrators  
can go to any of the user pages as needed. When that happens the two  
session variables - :admin and :user - get set. The issue is that  
sometimes the admins dont “logout” of those user pages and the :user  
session remain set - they simply close the form. Is there a way to set  
those sessions to nil whenever that window is closed if they dont  
logout? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/2 "2008-10-29T22:08:10Z")

</div>

On 29 Oct 2008, at 20:22, Shandy N. wrote:

> logout? Thanks,

Not in a fool proof way. You could have a window’s onClose fire off an  
ajax request, but obviously that won’t work if they have javascript  
turned off, won’t work if they just turn off their computer or if  
their broadband dies, might not work if they quit their browser etc…

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/3 "2008-10-29T22:12:42Z")

</div>

Frederick C. wrote:

> On 29 Oct 2008, at 20:22, Shandy N. wrote:
> 
> > logout? Thanks,
> 
> Not in a fool proof way. You could have a window’s onClose fire off an  
> ajax request, but obviously that won’t work if they have javascript  
> turned off, won’t work if they just turn off their computer or if  
> their broadband dies, might not work if they quit their browser etc…
> 
> Fred

That’s what I am trying to do by saying:

onunload = "delete\_cookie(‘user’);

The probablem is doesn’t seem to be working. When I say session[:user] =  
“blah” is the name of that cookie “user”?

If anyone is interested by delete\_cookie fuunction looks like:

function delete\_cookie ( cookie\_name )  
{  
var cookie\_date = new Date ( ); // current date & time  
cookie\_date.setTime ( cookie\_date.getTime() - 1 );  
document.cookie = cookie\_name += “=; expires=” +  
cookie\_date.toGMTString();  
}

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/4 "2008-10-29T22:32:13Z")

</div>

On 29 Oct 2008, at 21:12, Shandy N. wrote:

> > their broadband dies, might not work if they quit their browser  
> > “blah” is the name of that cookie “user”?

No. There is a single cookie (the default name is something like  
app\_session where app is the name of the application). The associated  
value is either a serialized ruby object containing the session (if  
you are using the cookiestore) or just some completely opaque id that  
rails can use to grab the session from the db/memcache/filesystem.  
(You could have worked some of this out just by looking in your  
browser’s settings - you can list all cookies stored by your browser)

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/5 "2008-10-29T23:01:29Z")

</div>

Frederick C. wrote:

> The associated  
> value is either a serialized ruby object containing the session (if  
> you are using the cookiestore) or just some completely opaque id that  
> rails can use to grab the session from the db/memcache/filesystem.  
> (You could have worked some of this out just by looking in your  
> browser’s settings - you can list all cookies stored by your browser)
> 
> Fred

I just upgraded to Rails 2.1.0. so I am using the cookiestore but I am  
storing id’s in multiple session variables: session[:user],  
session[:admin], session[:company\_id]. I am trying to destroy the  
session[:user] via a javaScript function.

I was under the impression that when I create a session[:user] =  
@user.id that the cookie looked like:

‘user=12232; expires=Thu, 29 Oct 2008 20:47:11 UTC; path=/’

and that I could erase it through javaScript by saying:

delete\_cookie( ‘user’ )

but Im not sure that when I create the session variable that the name is  
actually ‘user’?

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 29, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/6 "2008-10-29T23:34:22Z")

</div>

On 29 Oct 2008, at 22:01, Shandy N. wrote:

> and that I could erase it through javaScript by saying:
> 
> delete\_cookie( ‘user’ )
> 
> but Im not sure that when I create the session variable that the  
> name is  
> actually ‘user’?

You’re wrong. There is only one cookie. it contains a serialised ruby  
object (a hash to be quite precise)

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 30, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/setting-session-to-nil-when-window-is-closed/152075/7 "2008-10-30T15:26:24Z")

</div>

Frederick C. wrote:

> You’re wrong. There is only one cookie. it contains a serialised ruby  
> object (a hash to be quite precise)
> 
> Fred

I think I figured it out. I did a remote\_function in my layout

\<% func = remote\_function( :url =\> { :action =\> ‘set\_session\_nil’} )  
-%\>

"\> .......

and then in the controller I just set session[:user] = nil.

Sorry if I wasn’t understand correctly Fred, but thanks for your help. I  
learned a lot about sessions that I didn’t know and hopefully will be  
better prepared the next time I have to do something new and unusual  
with them.
