LoginSignup
1
1

More than 5 years have passed since last update.

cookieの有効期限

Last updated at Posted at 2017-09-06

RubyでCGIを作っていてcookieの有効期限がうまく設定されなかったので覚書。

問題点

Chromeでクッキーがセッション終わりで消えてた。次の日とかにアクセスすると前保存したクッキーが読めずに新規アクセスとみなされてしまう。

当時のコード

before1.rb
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => true,
                           'session_key' => '_my_session',
                           'tmpdir' => './tmp',
                           'session_expires' => Time.now + 2_592_000)  # 30days
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
Set-Cookie: _my_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/; expires=Fri, 06 Oct 2017 03:15:07 GMT
=end
before2.rb
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => false,
                           'session_key' => '_my_session',
                           'tmpdir' => './tmp')
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
Set-Cookie: _my_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/
=end

色々試行錯誤した結果

どうも、cookieやsessionで期限をセットしてもだめらしい。
httpヘッダ生成時に期限を指定してExpiresの行を生成しないとダメっぽい。
セッションを張ったときしか期限を設定していなかったのが問題みたい。
2回目のアクセスで期限なし(=ブラウザ終了するまでが期限)に変わっていた。

対策したコード

after1.rb
# こっちは問題ない。
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => true,
                           'session_key' => '_my_session',
                           'tmpdir' => './tmp',
                           'session_expires' => Time.now + 2_592_000)  # 30days
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
Set-Cookie: _washcrus_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/; expires=Fri, 06 Oct 2017 03:15:07 GMT
=end
after2.rb
# こっちに問題有り
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => false,
                           'session_key' => '_my_session',
                           'tmpdir' => './tmp',
                           'session_expires' => Time.now + 2_592_000)  # 30days
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
Set-Cookie: _my_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/; expires=Fri, 06 Oct 2017 03:15:07 GMT
=end

差分

sabun1.patch
+# こっちは問題ない。
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => true,
                           'session_key' => '_my_session',
                           'tmpdir' => './tmp',
                           'session_expires' => Time.now + 2_592_000)  # 30days
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
Set-Cookie: _washcrus_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/; expires=Fri, 06 Oct 2017 03:15:07 GMT
=end
sabun2.patch
+# こっちに問題有り
cgi = CGI.new
session = CGI::Session.new(cgi,
                           'new_session' => false,
                           'session_key' => '_my_session',
-                          'tmpdir' => './tmp')
+                          'tmpdir' => './tmp',
+                          'session_expires' => Time.now + 2_592_000)  # 30days
print cgi.header('charset' => 'UTF-8')

=begin
headerの内容:
Content-Type: text/html; charset=UTF-8
-Set-Cookie: _washcrus_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/
+Set-Cookie: _washcrus_session=d4bdaedea43795ee3bff44d1d73f9224; path=/~username/cgi-bin/; expires=Fri, 06 Oct 2017 03:15:07 GMT
=end

反省

cgi.header('expires' => Time.now)はページのキャッシュの期限設定でした。

参考

CGI
CGI::Session

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1