# CGI::Session weird behavior

**URL:** <https://www.ruby-forum.com/t/cgi-session-weird-behavior/173776>\
**Category:** Ruby\
**Created:** [August 14, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/cgi-session-weird-behavior/173776 "2009-08-14T15:55:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [August 14, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/cgi-session-weird-behavior/173776/1 "2009-08-14T15:55:42Z")

</div>

Hi,  
If I uncomment line 14 nothing is displayed and each time the page is  
refreshed ( 2 second interval) ruby creates a different file  
/tmp/rubysess.\*  
But if I uncomment lines 17-23 it works, displaying the last access time  
and only one session file is created.  
Why is that? I don’t want to use cgi.out{} and stuff ☹

1 \<%  
2 require ‘cgi’  
3 require ‘cgi/session’  
4 require ‘cgi/session/pstore’  
5 cgi = CGI.new(“html4”)  
6  
7 sess = CGI::Session.new( cgi, ‘database\_manager’ =\>  
CGI::Session::PStore,  
8 “session\_key” =\> “a\_test”,  
9 “session\_expires” =\> Time.now + 1 \*  
60,  
10 “prefix” =\> “rubysess.”)  
11 lastaccess = sess[“lastaccess”].to\_s  
12 sess[“lastaccess”] = Time.now  
13  
14 #print ‘’+lastaccess+’’  
15  
16 =begin  
17 cgi.out{  
18 cgi.html {  
19 cgi.body(){  
20 “Last access time: #{lastaccess}”  
21 }  
22 }  
23 }  
24 =end  
25  
26 sess.close  
27  
28 %\>

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [August 14, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/cgi-session-weird-behavior/173776/2 "2009-08-14T18:10:59Z")

</div>

On Aug 14, 9:55 am, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## 3 require ‘cgi/session’ 12 sess[“lastaccess”] = Time.now 23 } 24 =end 25 26 sess.close 27 28 %\>
> 
> Posted viahttp://www.ruby-forum.com/.

If don’t want to use cgi.out, you’d need to at least send back some  
http headers (at least Content-type) for anything to reach the user.  
(also, I don’t know why you’re using cgi = CGI.new(“html4”) if you  
don’t want the html helpers, but :\ )

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [August 15, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/cgi-session-weird-behavior/173776/3 "2009-08-15T15:20:42Z")

</div>

pharrington wrote:

> On Aug 14, 9:55ï¿½am, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## ï¿½ 3 require ‘cgi/session’ ï¿½12 sess[“lastaccess”] = Time.now ï¿½23 } ï¿½24 =end ï¿½25 ï¿½26 sess.close ï¿½27 ï¿½28 %\>
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> If don’t want to use cgi.out, you’d need to at least send back some  
> http headers (at least Content-type) for anything to reach the user.  
> (also, I don’t know why you’re using cgi = CGI.new(“html4”) if you  
> don’t want the html helpers, but :\ )

The first thing you should output is:

1. 

print “Content-type: text/html\r\n\r\n”

or equivalently:

1. 

puts cgi.header

1. Or you can also use cgi.out, which will automatically take care of  
the header information, like this:

require ‘cgi’

cgi = CGI.new

html =\<\<ENDOFHTML

Test
### hello world
ENDOFHTML

cgi.out do  
html  
end
