参考地址
设置和获取 Cookie | Gin Web Framework (gin-gonic.com)https://gin-gonic.com/zh-cn/docs/examples/cookie/
什么是cookie
cookie在互联网上随处可见,具体体现如下:
保持登录状态保存浏览器的历史记录大数据随心配,按喜好推送讯息购物网站加入购物车
都会用到cookie来保存一些信息;
Cookie 介绍
- HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分两次请求是否由同一个客户端发出
-
HTTP 是无状态协议。简单地说,当你浏览了一个页面,然后转到同一个网站的另一个页面,服务器无法认识到这是同一个浏览器在访问同一个网站。每一次的访问,都是没有任何关系的。如果我们要实现多个页面之间共享数据的话我们就可以使用 Cookie 或者 Session 实现
-
cookie 是存储于访问者计算机的浏览器中。可以让我们用同一个浏览器访问同一个域名的时候共享数据。
Cookie的用途
- 测试服务端发送cookie给客户端,客户端请求时携带cookie
设置 Cookie
Set-Cookie将Set-Cookie标头添加到ResponseWriter的标头中。提供的cookie必须有一个有效的名称。无效的cookie可能会被无提示地删除。
ctx.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
第一个参数 key第二个参数 value第三个参数 过期时间 . 如果只想设置 Cookie 的保存路径而不想设置存活时间,可以在第三个参数中传递 nil第四个参数 cookie 的路径第五个参数 cookie 的路径 Domain 作用域 本地调试配置成 localhost , 正式上线配置成域名第六个参数是 secure ,当 secure 值为 true 时, cookie 在 HTTP 中是无效,在 HTTPS 中才有效第七个参数 httpOnly ,是微软对 COOKIE 做的扩展。如果在 COOKIE 中设置了 “httpOnly” 属性,则通过程序(JS 脚本、 applet 等)将无法读取到 COOKIE 信息,防止 XSS 攻击产生
获取 Cookie
cookie, err := ctx.Cookie("name")
1、实操演练(设置获取cookie)
1、控制器设置
package test
import (
"fmt"
"gindemo04/models"
"net/http"
"github.com/gin-gonic/gin"
)
type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
//设置cookie
c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
fmt.Println(models.UnixToTime(1757814260))
c.HTML(http.StatusOK, "default/index.html", gin.H{
"msg": "model测试内容--转换时间戳---如下:",
"t": 1757814260,
})
}
func (con ModelTest) News(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
delay ,_:=c.Cookie("delay")
c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}
func (con ModelTest) Gouwu(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
c.String(http.StatusOK, "cookie="+username)
}
2、路由配置
package routers
import (
"gindemo04/controllers/test"
"github.com/gin-gonic/gin"
)
func DefaultRouters(r *gin.Engine) {
defaultRouters := r.Group("/")
{
defaultRouters.GET("/", test.ModelTest{}.Index)
defaultRouters.GET("/news", test.ModelTest{}.News)
defaultRouters.GET("/gouwu", test.ModelTest{}.Gouwu)
}
}
3、 测试
注意:这里为了方便,将main.go的端口设置为了80
1、先去setcookie
2、再去测试cookie
2、删除cookie的操作
可以设置如下:
1、控制器做如下配置
package test
import (
"fmt"
"gindemo04/models"
"net/http"
"github.com/gin-gonic/gin"
)
type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
//设置cookie
c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
//过期cookie设置
c.SetCookie("delay", "过期演示", 3, "/", "localhost", false, true)
fmt.Println(models.UnixToTime(1757814260))
c.HTML(http.StatusOK, "default/index.html", gin.H{
"msg": "model测试内容--转换时间戳---如下:",
"t": 1757814260,
})
}
func (con ModelTest) News(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
delay ,_:=c.Cookie("delay")
c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}
func (con ModelTest) Gouwu(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
c.String(http.StatusOK, "cookie="+username)
}
func (con ModelTest) Deletcookie(c *gin.Context) {
//删除cookie
c.SetCookie("username", "卫宫士郎", -1, "/", "localhost", false, true)
c.String(http.StatusOK, "删除cookie完毕,查看gouwu以及news是否还存在cookie的值")
}
2、路由配置如下
package routers
import (
"gindemo04/controllers/test"
"github.com/gin-gonic/gin"
)
func DefaultRouters(r *gin.Engine) {
defaultRouters := r.Group("/")
{
defaultRouters.GET("/", test.ModelTest{}.Index)
defaultRouters.GET("/news", test.ModelTest{}.News)
defaultRouters.GET("/gouwu", test.ModelTest{}.Gouwu)
defaultRouters.GET("/deletcookie", test.ModelTest{}.Deletcookie)
}
}
3、测试
再去查看gouwu就没有cookie了
3、过期cookie的操作
设置时间短一些
package test
import (
"fmt"
"gindemo04/models"
"net/http"
"github.com/gin-gonic/gin"
)
type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
//设置cookie
c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
//过期cookie设置
c.SetCookie("delay", "过期演示", 5, "/", "localhost", false, true)
//二级域名cookie共享(联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作)
fmt.Println(models.UnixToTime(1757814260))
c.HTML(http.StatusOK, "default/index.html", gin.H{
"msg": "model测试内容--转换时间戳---如下:",
"t": 1757814260,
})
}
func (con ModelTest) News(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
delay ,_:=c.Cookie("delay")
c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}
func (con ModelTest) Gouwu(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
c.String(http.StatusOK, "cookie="+username)
}
func (con ModelTest) Deletcookie(c *gin.Context) {
//删除cookie
c.SetCookie("username", "卫宫士郎", -1, "/", "localhost", false, true)
c.String(http.StatusOK, "删除cookie完毕,查看gouwu以及news是否还存在cookie的值")
}
1、测试:
再次刷新 ,没了
多个二级域名共享 cookie
举个例子:
联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作
分别把 a.frank.com 和 b.frank.com 解析到我们的服务器我们想的是用户在 a.frank.com 中设置 Cookie 信息后在 b.frank.com 中获取刚才设置的cookie,也就是实现多个二级域名共享 cookie
在本机操作域名解析的话,需要修改C:\Windows\System32\drivers\etc下的hosts文件
最后是这个效果
1、 控制器配置
package test
import (
"fmt"
"gindemo04/models"
"net/http"
"github.com/gin-gonic/gin"
)
type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
//二级域名cookie共享(联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作)
c.SetCookie("username","卫宫士郎",3600,"/",".frank.com",false,true)
fmt.Println(models.UnixToTime(1757814260))
c.HTML(http.StatusOK, "default/index.html", gin.H{
"msg": "model测试内容--转换时间戳---如下:",
"t": 1757814260,
})
}
func (con ModelTest) News(c *gin.Context) {
//获取cookie
username,_ := c.Cookie("username")
c.String(http.StatusOK, "cookie="+username)
}
2、路由配置
package routers
import (
"gindemo04/controllers/test"
"github.com/gin-gonic/gin"
)
func DefaultRouters(r *gin.Engine) {
defaultRouters := r.Group("/")
{
defaultRouters.GET("/", test.ModelTest{}.Index)
defaultRouters.GET("/news", test.ModelTest{}.News)
}
}
3、测试一下两个frank是否可以共享cookie
1、先去set cookie
2、然后去访问news(两个frank)
a.frank.com可以
b.frank.com也可以