From: "Ara.T.Howard" Date: 2003-12-19T06:21:57+09:00 Subject: Re: CGI::Session on mod_ruby On Fri, 19 Dec 2003, John Long wrote: > Date: Fri, 19 Dec 2003 02:43:38 +0900 > From: John Long > Newsgroups: comp.lang.ruby > Subject: CGI::Session on mod_ruby > > The following script is a simple page counter that works well for me running > as an rhtml file on mod_ruby: > > <% > require 'cgi/session' > > cgi = CGI.new('html4') > > session = CGI::Session.new(cgi) > > session['count'] ||= 0 > session['count'] = session['count'].to_i + 1 > > # content must be output in this way in order for headers to be written: > cgi.out { "count: " + session['count'] } > > session.close > %> > > However, if I replace the line "cgi.out ..." with "puts session['count']" > the script fails incrediment indicating that a new session is being created > every time. I would assume this is because the right headers (cookies, > etc...) are not written out in the response. If you use CGI::Session are you > forced to use cgi.out rather than your own output method? from cgi/session.rb .... # Most session state is maintained on the server. However, a session # id must be passed backwards and forwards between client and server # to maintain a reference to this session state. # # The simplest way to do this is via cookies. The CGI::Session class # provides transparent support for session id communication via cookies # if the client has cookies enabled. # # If the client has cookies disabled, the session id must be included # as a parameter of all requests sent by the client to the server. The # CGI::Session class in conjunction with the CGI class will transparently # add the session id as a hidden input field to all forms generated # using the CGI#form() HTML generation method. No built-in support is # provided for other mechanisms, such as URL re-writing. The caller is # responsible for extracting the session id from the session_id # attribute and manually encoding it in URLs and adding it as a hidden # input to HTML forms created by other mechanisms. Also, session expiry # is not automatically handled. .... that being said, you can always simply: require 'cgi' require 'cgi/session' cgi = CGI.new session = CGI::Session.new(cgi = CGI.new) c = (session['count'] ||= 0) printf "%s\ncount: %s\n", cgi.header, c session['count'] = session['count'].succ session.close this is all CGI#out really does anyhow. -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================