CGI::Session weird behavior

B

Bruno Sousa

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 '<HTML><BODY>'+lastaccess+'</BODY></HTML>'
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 %>
 
P

pharrington

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 '<HTML><BODY>'+lastaccess+'</BODY></HTML>'
 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 %>

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 :\ )
 
7

7stud --

pharrington said:
� 3 require 'cgi/session'
�12 sess["lastaccess"] = Time.now
�23 }
�24 =end
�25
�26 sess.close
�27
�28 %>

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:

2)
puts cgi.header


3) 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
<html>
<head><title>Test</title>
</head>
<body>
<h3>hello world</h3>
</body>
</html>
ENDOFHTML

cgi.out do
html
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top