# Simple questions regarding the session variable

**URL:** <https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565>\
**Category:** Rails\
**Created:** [December 16, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565 "2006-12-16T17:11:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 16, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/1 "2006-12-16T17:11:47Z")

</div>

I am seriving my webapp via WEBrick.

I start a Firefox browser #1. Somewhere along the way it performs:

```
        @session[:key] = 'A'

```

I start a second Firefox browser #2. Somewhere along the way it  
performs:

```
        @session[:key] = 'B'

```

What I notice: the value of key in browser #1 is now B not A. I check  
under  
RAILS\_ROOT/tmp/session and find a single session file only (where RAILS  
is using PStore to write the session data)

So whether the browsers are started concurrently, overlapping, or one  
at  
a time, the same underlying session is being used. Surely the web  
system  
is smart enough to generate different session numbers even if the  
client  
browser is on the same IP? Or am I being the idiot?

How in the heck can I generate multiple sessions one per client on  
WEBrick?  
Will that technique work under Apache or Mongrel which I intend to  
serve the  
production version of the site.

I attempted to look under

[http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions](http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions)

but that and related documentation has been down for a while.

Shane

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 16, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/2 "2006-12-16T18:03:28Z")

</div>

this has nothing to do with rails and everything to do with session  
implementation in browsers. Session is one array to the browser. If  
you open a new window or tab in the same browser, it gets the same  
session information. This is why you are able to login to, lets say  
Yahoo Mail, and open a new window and have the mail page open instead of  
the login screen. This is how it is supposed to work. If you need to  
check 2 different values for the same session key you should use 2  
different browsers (FF, Safari, IE , whatever).

peace

–jake

shane wrote:

> I am seriving my webapp via WEBrick.
> 
> I start a Firefox browser #1. Somewhere along the way it performs:
> 
> ```
> @session[:key] = 'A'
> 
> ```
> 
> I start a second Firefox browser #2. Somewhere along the way it  
> performs:
> 
> ```
> @session[:key] = 'B'
> 
> ```
> 
> What I notice: the value of key in browser #1 is now B not A. I check  
> under  
> RAILS\_ROOT/tmp/session and find a single session file only (where RAILS  
> is using PStore to write the session data)
> 
> So whether the browsers are started concurrently, overlapping, or one  
> at  
> a time, the same underlying session is being used. Surely the web  
> system  
> is smart enough to generate different session numbers even if the  
> client  
> browser is on the same IP? Or am I being the idiot?
> 
> How in the heck can I generate multiple sessions one per client on  
> WEBrick?  
> Will that technique work under Apache or Mongrel which I intend to  
> serve the  
> production version of the site.
> 
> I attempted to look under
> 
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions)
> 
> but that and related documentation has been down for a while.
> 
> Shane

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 16, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/3 "2006-12-16T18:27:53Z")

</div>

shane wrote:

> ```
> @session[:key] = 'B'
> 
> ```
> 
> What I notice: the value of key in browser #1 is now B not A.  
> [snip]

This is happening because Rails and your browser identify the session by  
its \_session\_id value stored in the browser cookie.

Another way to do this is to turn off the browser cookies support and  
use  
my plugin for session persistent. Here each time you establish a new  
connection  
a new session is created, even within the same browser (new tab) or new  
browser  
window.

Long  
www.edgesoft.ca/blog/read/2 - rails articles

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 16, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/4 "2006-12-16T23:24:47Z")

</div>

Jake V. wrote:

> this has nothing to do with rails and everything to do with session  
> implementation in browsers.

FYI, I’ve found that this is all very browser dependent. My  
recollection from looking at this a few months ago was something like  
this:

Mac Safari: each window has its own session  
Mac Firefox: just one session for all windows/tabs

Windows IE: each window has its own session

Linux firefox: with tabs, they all have the same session, separate  
windows have different sessions

For safety, the two browser recommendation is a good one. I was  
developing an app that had two different “users” that interacted with  
each other. Usually I could just use two different windows for the two  
different “users”, and everything was fine in Safari.

Then another team member went to try out my stuff and “everything was  
broken”. Finally realized it was because he was using firefox on linux  
with tabs for the two users.

One has to wonder how many millions of hacker-hours have been wasted  
over the past twenty years dealing with the  
incompatibilities/differences between the different computer platforms.

Probably if we had all that time back, we could use it to solve a big  
problem like world peace or how to get honest people to run for public  
office.

jp

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 17, 2006, 1:14am UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/5 "2006-12-17T01:14:35Z")

</div>

Jeff,

you’re prolly be right about Safari, but IE7 and 6 both share session &  
cookie data across windows, i have to deal with that mess every day 🙂

–jake

Jeff P. wrote:

> Jake V. wrote:
> 
> > this has nothing to do with rails and everything to do with session  
> > implementation in browsers.
> 
> FYI, I’ve found that this is all very browser dependent. My  
> recollection from looking at this a few months ago was something like  
> this:
> 
> Mac Safari: each window has its own session  
> Mac Firefox: just one session for all windows/tabs
> 
> Windows IE: each window has its own session
> 
> Linux firefox: with tabs, they all have the same session, separate  
> windows have different sessions
> 
> For safety, the two browser recommendation is a good one. I was  
> developing an app that had two different “users” that interacted with  
> each other. Usually I could just use two different windows for the two  
> different “users”, and everything was fine in Safari.
> 
> Then another team member went to try out my stuff and “everything was  
> broken”. Finally realized it was because he was using firefox on linux  
> with tabs for the two users.
> 
> One has to wonder how many millions of hacker-hours have been wasted  
> over the past twenty years dealing with the  
> incompatibilities/differences between the different computer platforms.
> 
> Probably if we had all that time back, we could use it to solve a big  
> problem like world peace or how to get honest people to run for public  
> office.
> 
> jp

---

<div class="post-metadata">

**Author:** ![shane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@shane](https://www.ruby-forum.com/u/shane)\
**Post date:** [December 21, 2006, 3:57am UTC](https://www.ruby-forum.com/t/simple-questions-regarding-the-session-variable/79565/6 "2006-12-21T03:57:19Z")

</div>

Thank you (and all responders). In the near future I will have to take  
Long’s advice and move off Pstore based session files anyway which are  
slow.
