【问题标题】:Golang Cookie Max-Age vs ExpireGolang Cookie Max-Age 与过期
【发布时间】:2022-01-14 19:56:08
【问题描述】:

Cookie struct 中的 Expires 和 Max-Age 有什么区别? 看不懂。

type Cookie struct {
    Name  string
    Value string

    Path       string    // optional
    Domain     string    // optional
    Expires    time.Time // optional
    RawExpires string    // for reading cookies only

    // MaxAge=0 means no 'Max-Age' attribute specified.
    // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
    // MaxAge>0 means Max-Age attribute present and given in seconds
    MaxAge   int
    Secure   bool
    HttpOnly bool
    SameSite SameSite
    Raw      string
    Unparsed []string // Raw text of unparsed attribute-value pairs
}

【问题讨论】:

标签: http go cookies expires-header max-age


【解决方案1】:

它们实际上是 Set-Cookie 标头的不同字段,并非特定于 Go。

来自Mozilla docs

过期

将 cookie 的最长生命周期表示为 HTTP 日期时间戳。有关所需格式,请参阅Date

如果未指定,则 cookie 变为 会话 cookie。当客户端关闭时会话结束,之后会话 cookie 被删除。

警告:许多网络浏览器都有会话恢复功能,可以保存所有选项卡并在下次使用浏览器时恢复它们。会话 cookie 也将被恢复,就像浏览器从未关闭一样。

当设置了Expires 日期时,截止日期与设置 cookie 的客户端相关,而不是服务器。

最大年龄

表示 cookie 过期前的秒数。零或负数将立即使 cookie 过期。如果ExpiresMax-Age 都设置了,则Max-Age 优先。

【讨论】:

  • 感谢 Dylan 的回答。现在我明白了。
猜你喜欢
  • 2015-05-02
  • 2017-02-09
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 2019-06-26
相关资源
最近更新 更多