# Problem with session under cgi file

**URL:** <https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700>\
**Category:** Ruby\
**Created:** [February 24, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700 "2007-02-24T20:04:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/1 "2007-02-24T20:04:29Z")

</div>

When i set a value in session after changing page session doesn’t  
remember the value.  
I have an example.  
Page: [http://darksky.pl/test/index.cgi](http://darksky.pl/test/index.cgi)  
Source: [http://darksky.pl/test/index.txt](http://darksky.pl/test/index.txt)  
Don’t ask why I have to make it with cgi ;/

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/2 "2007-02-24T20:15:08Z")

</div>

On Sun, 25 Feb 2007, Dsa Dang wrote:

> When i set a value in session after changing page session doesn’t  
> remember the value.  
> I have an example.  
> Page: [http://darksky.pl/test/index.cgi](http://darksky.pl/test/index.cgi)  
> Source: [http://darksky.pl/test/index.txt](http://darksky.pl/test/index.txt)  
> Don’t ask why I have to make it with cgi ;/

you need to make sure you flush and close it.

-a

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/3 "2007-02-24T20:35:12Z")

</div>

> you need to make sure you flush and close it.

I’ve added sess.close at the end but it still doesn’t work 😕

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/4 "2007-02-24T21:07:19Z")

</div>

On Sun, 25 Feb 2007, Dsa Dang wrote:

> > you need to make sure you flush and close it.
> 
> I’ve added sess.close at the end but it still doesn’t work 😕

are you setting the expiration?

cgi = CGI.new

session = CGI::Session.new(cgi, “session\_expires” =\>  
Time.at(2\*\*31-1))  
at\_exit{ session.update; session.close }

-a

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/5 "2007-02-24T21:26:21Z")

</div>

On 2/24/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> cgi = CGI.new
> 
> session = CGI::Session.new(cgi, “session\_expires” =\> Time.at(2\*\*31-1))  
> at\_exit{ session.update; session.close }

Good suggestions, but both should be unnecessary. CGI::Session uses a  
finalizer to ensure the session is closed and all the builtin session  
stores  
update on close. No expires means the cookie lives for the duration of  
the  
browser session.

Your source suggested you may be sending a malformed HTTP response.  
Inspect  
the request and response in Firebug to see: look for the Set-Cookie  
header  
in the response and the Cookie header in subsequent requests.

jeremy

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 24, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/6 "2007-02-24T22:18:30Z")

</div>

On Sun, 25 Feb 2007, Jeremy K. wrote:

> update on close. No expires means the cookie lives for the duration of the  
> browser session.

yeah i know - i had issues with this just last week though - i’ll try to  
reproduce…

-a

---

<div class="post-metadata">

**Author:** ![dsa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dsa](https://www.ruby-forum.com/u/dsa)\
**Post date:** [February 25, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/problem-with-session-under-cgi-file/86700/7 "2007-02-25T16:16:00Z")

</div>

So… my friend have found what was wrong. It’s all about session id.  
Here it must be send manually. I’ll leave the source here as it may be  
deleted from the server soon (first old source, then correct and  
working).

* * *

old:  
#!/usr/bin/ruby

require ‘cgi’  
require ‘cgi/session’  
require ‘cgi/session/pstore’

def mojaSesja(cgi)  
return CGI::Session.new(cgi,  
#‘database\_manager’ =\> CGI::Session::PStore, # use PStore  
‘session\_key’ =\> ‘rek\_key’, # custom session key  
‘session\_expires’ =\> Time.now + 30 \* 60, # 30 minute timeout  
‘prefix’ =\> ‘rek\_’) # PStore option  
end

cgi = CGI.new(“html4”)  
sess = mojaSesja(cgi)

puts “Content-type: text/html\n\n”  
puts “a  
”  
puts “b  
”

if cgi[‘z’] == ‘a’  
sess[‘z’] = cgi[‘z’]  
sess.update  
end

puts sess[‘z’]

* * *

new (ok):

#!/usr/bin/ruby

require ‘cgi’  
require ‘cgi/session’  
require ‘cgi/session/pstore’

def mojaSesja(cgi)  
CGI::Session.new(cgi,  
#‘database\_manager’ =\> CGI::Session::PStore, # use PStore  
‘session\_key’ =\> ‘session\_id’, # custom session key  
‘session\_expires’ =\> Time.now + 30 \* 60, # 30 minute timeout  
‘prefix’ =\> ‘rek\_’) # PStore option  
end

cgi = CGI.new(“html4”)  
sess = mojaSesja(cgi)

puts “Content-type: text/html\n\n”  
print “a  
”  
print “b  
”

if cgi[‘z’] == ‘a’  
sess[‘z’] = cgi[‘z’]  
end

puts sess[‘z’]

sess.close
