Ruby CGI::Session で Cookie が発行されない

February 29, 2008

ログイン CGI でクッキーがクライアントに返らないので調べたら・・・

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby
require 'cgi/session'
…(省略)…
def create_session(cgi, timeout)
begin
\# 既存のセッションは削除
session = CGI::Session.new(cgi, {
'new_session' =\> false })
session.delete
rescue ArgumentError
end
return CGI::Session.new(cgi, {
'new_session' =\> true,
'session_expires' =\> timeout })
end
end
session = create_session(cgi, Time.now + 3600)
session\['savadata'\] = "hogehoge"
print "Content-Type:text/html\\r\\n\\r\\n"
print <<EOF
<html>
<body>
<p>login success.</p>
</body>
</html>
EOF
exit 0

としたらクッキーができない。
ヘッダの出力がないことに気づいた。

1
2
3
4
5
6
7
8
puts cgi.header({ 'Content-Type' =\> 'text/html'})
print <<EOF
<html>
<body>
<p>login success.</p>
</body>
</html>
EOF

として cgi.header を puts しなければいけなかった。

tilfin freelance software engineer