Cookies

原文:https://gobuffalo.io/documentation/request_handling/cookies/

Cookies Cookie

An HTTP cookie is a small piece of data that a server sends to the user’s web browser. The browser can store this data and send it back to the same server, even after the browser restart (unlike a browser session).

​ HTTP cookie 是服务器发送给用户网络浏览器的少量数据。浏览器可以存储此数据,并在浏览器重新启动后将其发送回同一服务器(与浏览器会话不同)。

(HTTP) cookies are commonly used to save users state (like whether the user logged-in). See https://golang.org/pkg/net/http/#Cookie for more information on cookies in Go.

​ (HTTP) cookie 通常用于保存用户状态(例如用户是否已登录)。有关 Go 中 cookie 的更多信息,请参阅 https://golang.org/pkg/net/http/#Cookie

1
2
3
4
5
func MyHandler(c buffalo.Context) error {
  // ...
  c.Cookies().Set("user_id", user.ID, 30 * 24 * time.Hour)
  // ...
}
1
2
3
4
5
6
func MyHandler(c buffalo.Context) error {
  // ...
  exp := time.Now().Add(365 * 24 * time.Hour) // expire in 1 year
  c.Cookies().SetWithExpirationTime("user_id", user.ID, exp)
  // ...
}
1
2
3
4
5
func MyHandler(c buffalo.Context) error {
  // ...
  c.Cookies().SetWithPath("user_id", user.ID, "/user")
  // ...
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import "net/http"
func MyHandler(c buffalo.Context) error {
  // ...
  ck := http.Cookie{
    Name:    "token",
    Value:   token,
    Path:    "/",
    Expires: time.Now().Add(30 * 24 * time.Hour), // expire in 1 month
  }

  http.SetCookie(c.Response(), &ck)
  // ...
}

See Cookie struct for other parameters.

​ 有关其他参数,请参阅 Cookie 结构。

1
2
3
4
5
6
7
func MyHandler(c buffalo.Context) error {
  value, err := c.Cookies().Get("user_id")
  if err != nil {
    return err
  }
  return c.Render(200, r.String(value))
}
1
2
3
4
func MyHandler(c buffalo.Context) error {
  c.Cookies().Delete("user_id")
  // ...
}
最后修改 October 10, 2024: 更新 (a4b8f85)