Golang中的Cookie Max-Age与Expire的区别是什么?

huangapple go评论119阅读模式
英文:

Golang Cookie Max-Age vs Expire

问题

Expires和Max-Age在Cookie结构中的区别是什么?
我无法理解。

type Cookie struct {
Name string
Value string

Path       string    // 可选
Domain     string    // 可选
Expires    time.Time // 可选
RawExpires string    // 仅用于读取cookie

// MaxAge=0表示未指定'Max-Age'属性。
// MaxAge<0表示立即删除cookie,相当于'Max-Age: 0'
// MaxAge>0表示Max-Age属性存在,并以秒为单位给出
MaxAge   int
Secure   bool
HttpOnly bool
SameSite SameSite
Raw      string
Unparsed []string // 未解析的属性-值对的原始文本

}

英文:

What is differencies between Expires and Max-Age in Cookie struct ?
I cannot understand.

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 &#39;Max-Age&#39; attribute specified.
	// MaxAge&lt;0 means delete cookie now, equivalently &#39;Max-Age: 0&#39;
	// MaxAge&gt;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
}

答案1

得分: 15

它们实际上是Set-Cookie头的不同字段,与Go无关。

根据Mozilla文档

Expires
> 表示cookie的最大生存时间,使用HTTP日期时间戳进行表示。有关所需的格式,请参见Date

> 如果未指定,cookie将成为会话cookie。会话在客户端关闭后结束,此后会话cookie将被删除。

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

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

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

英文:

They are actually different fields of the Set-Cookie header, not specific to Go.

From the Mozilla docs:

Expires
> Indicates the maximum lifetime of the cookie as an HTTP-date timestamp. See Date for the required formatting.

>If unspecified, the cookie becomes a session cookie. A session finishes when the client shuts down, after which the session cookie is removed.

>Warning: Many web browsers have a session restore feature that will save all tabs and restore them the next time the browser is used. Session cookies will also be restored, as if the browser was never closed.

>When an Expires date is set, the deadline is relative to the client the cookie is being set on, not the server.

Max-Age
> Indicates the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence.

huangapple
  • 本文由 发表于 2022年1月15日 03:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70715904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定