英文:
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 '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
}
答案1
得分: 15
它们实际上是Set-Cookie头的不同字段,与Go无关。
根据Mozilla文档:
Expires
> 表示cookie的最大生存时间,使用HTTP日期时间戳进行表示。有关所需的格式,请参见Date。
> 如果未指定,cookie将成为会话cookie。会话在客户端关闭后结束,此后会话cookie将被删除。
> **警告:**许多Web浏览器都有会话恢复功能,可以保存所有选项卡并在下次使用浏览器时恢复它们。会话cookie也将被恢复,就好像浏览器从未关闭过一样。
> 当设置了Expires日期时,截止日期是相对于设置cookie的客户端而言的,而不是服务器。
Max-Age
> 表示cookie过期的秒数。零或负数将立即使cookie过期。如果同时设置了Expires和Max-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论