Introduction

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#Cookie

Common Methods:

  1. type Cookie
  2. func GetCookie(r *Request) *Cookie
  3. func (c *Cookie) Contains(key string) bool
  4. func (c *Cookie) Flush()
  5. func (c *Cookie) Get(key string, def ...string) string
  6. func (c *Cookie) GetSessionId() string
  7. func (c *Cookie) Map() map[string]string
  8. func (c *Cookie) Remove(key string)
  9. func (c *Cookie) RemoveCookie(key, domain, path string)
  10. func (c *Cookie) Set(key, value string)
  11. func (c *Cookie) SetCookie(key, value, domain, path string, maxAge time.Duration, httpOnly ...bool)
  12. func (c *Cookie) SetHttpCookie(httpCookie *http.Cookie)
  13. func (c *Cookie) SetSessionId(id string)

The Cookie object corresponding to the current request can be obtained at any time through the *ghttp.Request object since both Cookie and Session are related to the request session and are therefore member objects of ghttp.Request, accessible to the public. The Cookie object does not need to be manually closed; the HTTP Server will automatically close it after the request cycle ends.

Additionally, Cookie encapsulates two methods related to SessionId:

  1. Cookie.GetSessionId() is used to obtain the SessionId submitted by the current request. Each request’s SessionId is unique and accompanies the entire request cycle. The value may be empty.
  2. Cookie.SetSessionId(id string) is used to custom-set the SessionId into the Cookie, which is then returned to the client (often the browser) for storage. Subsequently, the client can carry the SessionId in Cookie with each request.

When setting the Cookie variable, an expiration time can be given, which is an optional parameter. The default Cookie expiration time is one year.

Cookie - 图1tip

The default storage name for SessionId in Cookie is gfsession.

Usage Example

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. )
  7. func main() {
  8. s := g.Server()
  9. s.BindHandler("/cookie", func(r *ghttp.Request) {
  10. datetime := r.Cookie.Get("datetime")
  11. r.Cookie.Set("datetime", gtime.Datetime())
  12. r.Response.Write("datetime:", datetime)
  13. })
  14. s.SetPort(8199)
  15. s.Run()
  16. }

Execute the outer main.go, and try refreshing the page http://127.0.0.1:8199/cookie, the displayed time keeps changing.

For controller objects, many session-related object pointers inherited from the base class controller can be regarded as aliases and used directly, all pointing to the same object:

  1. type Controller struct {
  2. Request *ghttp.Request // Request data object
  3. Response *ghttp.Response // Response data object (r.Response)
  4. Server *ghttp.Server // WebServer object (r.Server)
  5. Cookie *ghttp.Cookie // COOKIE operation object (r.Cookie)
  6. Session *ghttp.Session // SESSION operation object
  7. View *View // View object
  8. }

Since Cookie is already a very familiar component to web developers, and the related API is very simple, further elaboration is not needed here.

The default validity period for Cookie is 1 year. If we want the Cookie to expire with the user’s browsing session, like this:

Cookie - 图2

Then we only need to set the Cookie key-value pair via SetCookie and set the maxAge parameter to 0. Like this:

  1. r.Cookie.SetCookie("MyCookieKey", "MyCookieValue", "", "/", 0)