cookieの有効期限
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)はページのキャッシュの期限設定でした。
参考
Author And Source
この問題について(cookieの有効期限), 我々は、より多くの情報をここで見つけました https://qiita.com/nbjiao/items/5b5c985a0be7bc6b0b1a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .