打赏

Beego Session使用

Beego Session使用

import (
	"github.com/astaxie/beego/session"
)

// 初始化一个全局的变量用来存储 session 控制器
var globalSessions *session.Manager

//入口函数中初始化数据
func init() {
	globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":3600, "maxLifetime": 3600, "secure": false, "sessionIDHashFunc": "sha1", "sessionIDHashKey": "", "cookieLifeTime": 3600, "providerConfig": ""}`)
	go globalSessions.GC()
}

func login(w http.ResponseWriter, r *http.Request) {
	sess, _ := globalSessions.SessionStart(w, r)
	defer sess.SessionRelease(w)
	username := sess.Get("username")
	if r.Method == "GET" {
		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, nil)
	} else {
		sess.Set("username", r.Form["username"])
	}
}
posted @ 2020-04-02 13:53  苍山落暮  阅读(810)  评论(0编辑  收藏  举报